1. 程式人生 > 實用技巧 >JAVA的學習日記15

JAVA的學習日記15

重寫

重寫:需要有繼承關係,子類重寫父類的方法
1.是非靜態的【要著重區別和靜態的區別】
2.方法必須相同
3.引數列表必須相同
4.修飾符:範圍可以擴大但不能縮小:public>protected>default>private
5.丟擲的異常:範圍可以被縮小,但不能擴大:ClassNotFoundException---->Exception(大)

重寫,子類的方法和父類必須要一致,方法體不同!

為什麼要重寫?
    1.父類的功能,子類不一定需要,或者不一定滿足!
    2.alt+insert:override;
Application
package com.oop;

import com.oop.Demo03.Pet;
import com.oop.Demo05.A;
import com.oop.Demo05.B;
import com.oop.Demo05.Student;

//一個專案應該只有一個main方法
public class Application {
    public static void main(String[] args) {

        //靜態方法和非靜態方法區別很大!所以不能亂用
        A a = new A();
        //方法的呼叫只和左邊,定義的資料型別有關
        //靜態方法時:A-->test()
        //非靜態方法時:A-->test()
        a.test();

        //父類的引用指向了子類
        B b = new A();//子類重寫了父類的方法
        //靜態方法時:B-->test()
        //非靜態方法時:A-->test()
        b.test();
    }
}


A:
package com.oop.Demo05;

public class A extends B {
    //

    //Override  重寫
    @Override   //註解:有功能的註釋!
    public void test() {
        System.out.println("A-->test()");
        //super.test();
    }
}




B:
package com.oop.Demo05;

//重寫都是方法的重寫,與屬性無關
public class B {
    public  void test(){
        System.out.println("B-->test()");
    }
}

多型

  • 即同一方法可以根據傳送物件的不同而採用多種不同的行為方式

  • 一個物件的實際型別是確定的,但可以指向物件的引用的型別有很多

  • 多型存在的條件

有繼承關係
子類重寫父類的方法
父類引用指向子類物件

  • 注意:多型是方法的多型,屬性沒有多型性
  • instanceof--型別轉換【是屬於引用型別的轉換】
Application
package com.oop;
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;



public class Application {
    public static void main(String[] args) {
        //一個物件的實際型別是確定的,但實際使用還是要看左邊的引用型別
        //Student student = new Student();
        //Person person = new Person();


        //可以指向的引用型別就不確定了,父類的引用指向子類
        //Student 能夠呼叫的方法都是自己的或者繼承父類的!
        Student s1 = new Student();
        //Person 父型別,可以指向子類,但是不能呼叫子類獨有的方法
        //如果想要呼叫,要使用特殊的方法【強制轉換】,如((Student)s2).子類的獨有的方法;
        Person s2 = new Student();
        Object s3 = new Student();

        s2.run();   //子類重寫父類的方法,執行子類的方法
        s1.run();

        //物件能夠執行哪些方法,主要是看物件左邊的型別,和右邊關係不大!
        s1.eat();
        ((Student)s2).eat();
    }
}




Student:
package com.oop.Demo06;

public class Student extends Person{
    @Override
    public void run() {

        System.out.println("Student");
        //super.run();
    }

    public void eat(){
        System.out.println("eat");
    }
}



Person:
package com.oop.Demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }

}

注意事項:

  • 多型的注意事項:
    1.多型是方法的多型,屬性沒有多型
    2.父類和子類要有聯絡【繼承】 型別轉換異常!ClassCastException!
    3.存在條件:繼承關係,方法需要重寫,父類引用指向子類物件! Father f1 = new Son();
  • 沒有多型【本身就不能重寫】的案例:
    1.static 方法,屬於類,他不屬於例項
    2.final 常量
    3.private 方法

Instanceof

判斷一個物件是什麼型別的

Application

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
//        Object object = new Student();
//
//        //X instanceof Y是否能夠通過編譯----看X和Y是否有父子關係
//
//        System.out.println(object instanceof Student);//true
//        System.out.println(object instanceof Person);//true
//        System.out.println(object instanceof Object);//true
//        System.out.println(object instanceof Teacher);//false
//        System.out.println(object instanceof String);//false
//
//        System.out.println("=====================");
//
//        Person person = new Student();
//        System.out.println(person instanceof Student);//true
//        System.out.println(person instanceof Person);//true
//        System.out.println(person instanceof Object);//true
//        System.out.println(person instanceof Teacher);//false
//        //System.out.println(person instanceof String);//編譯報錯
//
//        System.out.println("=====================");
//
//        Student student = new Student();
//        System.out.println(student instanceof Student);//true
//        System.out.println(student instanceof Person);//true
//        System.out.println(student instanceof Object);//true
//        //System.out.println(student instanceof Teacher);//編譯報錯
//        //System.out.println(student instanceof String);//編譯報錯

        //型別之間的轉換:父 與 子

        //高                 低
        Person obj = new Student();

        //父類--轉化為-->子類
        //Student將這個物件轉換為Student型別,我們就可以使用Student型別的方法了
//        Student student = (Student)obj;
//        student.go();

        //寫成一句話為
        ((Student)obj).go();


        //子類--轉化為-->父類  可能會丟失自己的一部分方法【自己獨特的那部分】
        Student student = new Student();
        student.go();
        Person person = student;    //student轉化為Person
    }
    
}



Student
public class Student extends Person{
    public void go(){
        System.out.println("Go");
    }
}


  • 小結:
    1.子類引用指向子類的物件
    2.把子類轉換為父類,向上轉型:直接轉就行,但是可能會丟失自己的一部分方法【自己獨特的那部分】
    3.把父類轉換為子類,向下轉型:強制轉換
    4.方便方法的呼叫,減少重複的程式碼!Java的特點之一---簡潔
  • Java三大特性:
    封裝、繼承、多型!
實踐是程式設計成功的最重要的一步!