class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun1(){}") ; } public void fun2(){ this.fun1() ; // 调用fun1()方法 } }; class B extends A{ public void fun1(){ // 此方法被子类覆写了 System.out.println("B --> public void fun1(){}") ; } public void fun3(){ System.out.println("B --> public void fun3(){}") ; } }; public class InstanceofDemo01{ public static void main(String asrgs[]){ A a1 = new B() ; // 通过向上转型实例化对象 System.out.println("A a1 = new B():" + (a1 instanceof A)) ; System.out.println("A a1 = new B():" + (a1 instanceof B)) ; A a2 = new A() ; // 通过向上转型实例化对象 System.out.println("A a2 = new B():" + (a2 instanceof A)) ; System.out.println("A a2 = new B():" + (a2 instanceof B)) ; } };
class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun1(){}") ; } public void fun2(){ this.fun1() ; // 调用fun1()方法 } }; class B extends A{ public void fun1(){ // 此方法被子类覆写了 System.out.println("B --> public void fun1(){}") ; } public void fun3(){ System.out.println("B --> public void fun3(){}") ; } }; class C extends A{ public void fun1(){ // 此方法被子类覆写了 System.out.println("C --> public void fun1(){}") ; } public void fun5(){ System.out.println("C --> public void fun5(){}") ; } }; public class InstanceofDemo02{ public static void main(String asrgs[]){ fun(new B()) ; fun(new C()) ; } public static void fun(A a){ a.fun1() ; if(a instanceof B){ B b = (B) a ; b.fun3() ; } if(a instanceof C){ C c = (C) a ; c.fun5() ; } } };
吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字
原文:https://www.cnblogs.com/tszr/p/12153021.html