A instanceof B:A是任意对象表达式,B是任意已定义的类。
------->判断A是否是B的实例对象或者B子类的实例对象。
class father
{
public void f()
{
System.out.println("father.f()");
}
}
class child extends father
{
public void f_c()
{
System.out.println("child.f1()");
}
}
public class bookInstanceof {
public static void main(String[] args) {
child c = new child();
father f = new father();
if(c instanceof child) //c是child的实例对象
{
c.f_c();
c.f();
}
if(c instanceof father) //c是father子类的实例对象
{
c.f_c();
c.f();
}
if(f instanceof child) //f不是child的实例对象
{
f.f();
}
if(f instanceof father) //f是father的实例对象
{
f.f();
}
}
}
输出结果是:
child.f1()
father.f()
child.f1()
father.f()
father.f()