方法 -- 繼承關係

public class test {
    void show() {
        System.out.println("父類別");
    } 
}
public class test2 extends test{
    void show() {
        System.out.println("子類別");
    }
    void Ha() {
        System.out.println("Ha");
    }
}
public class Gogo {
    public static void main(String[] args) {
        test T1 = new test();
        test2 T2 = new test2();
        
        T1.show();
        T2.show();
        
        T1 = T2;
        T1.show();
父子同樣具有show()時,將優先執行子類別
/父類別
/子類別
/子類別


public class test {

    void show(String i) {
        System.out.println("父類別");
   } 
}
public class test2 extends test{
    void show() {
        System.out.println("子類別");
    }
    void Ha() {
        System.out.println("Ha");
    }
}
public class Gogo {
    public static void main(String[] args) {
        test T1 = new test();
        test2 T2 = new test2();
        
        T1.show("ss");
        T2.show();
        
        T1 = T2; 
        T1.show("ss");
    }
}
子不具有show(String),往父類找,找到發法,執行父類別方法
/父類別
/子類別
/父類別

猜你喜欢

转载自www.cnblogs.com/pyleu1028/p/10339502.html
今日推荐