???????н?????£?
????This is Father’s method
????This is Father’s method
????This is Father’s method
????????????????????????????????method(Father father)????????????????????е?????????????????????????????????method(Father father)???????????????java???????????????
????instanceof???????????
?????????????????????????????????????????????????????????????????????????????????????java????????????????????????????????java?????????????????????????????????????ж?????????????instanceof??????????????????ж?????????method(Father father)??????????????????ж??????????????????????method(Father father)???????£?
????public void method(Father father){
????if(father instanceof Son1){
????method((Son1)father);
????}else if(father instanceof Son2){
????method((Son2)father);
????}else if(father instanceof Father){
????System.out.println("This is Father's method");
????}
????}
??????????????????ж????????????????????ж?????Father???????????????????????ж??Father?????????????ж???????????????????????????????????????????????????????????н?????£?
????This is Father’s method
????This is Son1′s method
????This is Son2′s method
??????????????????????????????????α????????????????????????????ж??????????Father??100??????????????????????????????????????????????????????????У?????????????????????????????
??????????????????
????????????????????????23?????????9???????????????????????????
??????A?е????method1??method2?????????method2??????ɡ????????????java?????????????????????????B????B?????showA(A a) ????????????У???????A??method1???????????A??method2?????е?????B??showA??????????????????????showA????????????????this?????????????????????????????????????????????????Ч??????????????????????????????????????????????????????£?
class Father {
public void accept(Execute exe){
exe.method(this);
}
}
class Son1 extends Father{
public void accept(Execute exe){
exe.method(this);
}
}
class Son2 extends Father{
public void accept(Execute exe){
exe.method(this);
}
}
class Execute {
public void method(Father father){
System.out.println("This is Father's method");
}
public void method(Son1 son){
System.out.println("This is Son1's method");
}
public void method(Son2 son){
System.out.println("This is Son2's method");
}
}
public class Test {
public static void main(String[] args){
Father father = new Father();
Father s1 = new Son1();
Father s2 = new Son2();
Execute exe = new Execute();
father.accept(exe);
s1.accept(exe);
s2.accept(exe);
}
}
?????????????????????????Father??Son1??Son2?з???????????????????????????????????Execute??method????????????????Father??accept?????????н?????£?
????This is Father’s method
????This is Son1′s method
????This is Son2′s method
???????н?????????????????????????????????????????????????????????????????????????и????????????????????????????????????????????instanceof????????Ч????????????instanceof????????????????????????????????????instanceof????????????????????????instanceof?????????????????????????????????????