一、instanceof
在编写代码过程中,如果不知道一个对象属于哪一个类,这时instanceof关键字起到决定性作用,他会以boolean(truefalse)反馈结果
class A{
public void fun1(){
System.out.println("A----->public void fun1");
}
public void fun2(){
this.fun1();
}
}
class B extends A{
public void fun1(){ //覆写父类fun1方法
System.out.println("B-->public void fun1(B)");
}
public void fun3(){
System.out.println("B---->public void fun3(B)");
}
}
class C extends A{
public void fun1(){ //覆写父类fun1方法
System.out.println("C---->public void fun1(C)");
}
public void fun5(){
System.out.println("C------>public void fun5(C)");
}
}
public class DomeA {
public static void main(String[] args){
A a1 = new B(); //通过向上转型实例化A类对象
System.out.println("A a1 = new B():"+(a1 instanceof A));
System.out.println("A a1 = new B():"+(a1 instanceof B));
A a2 = new A(); //通过A类的构造实例化本类对象
System.out.println("A a2 = new A():"+(a2 instanceof A));
System.out.println("A a2 = new A():"+(a2 instanceof B));
}
}
//A a1 = new B():true
//A a1 = new B():true
//A a2 = new A():true
//A a2 = new A():false