Java中instanceof的用法详解

interface A{
	
}
class B implements A{
	 
}
class C extends B{
	 
}
public class Iinstanceof {

	public static void main(String[] args) {
		A ab=new B();
		A ac=new C();
		B bc=new C();
		B bb=new B();
		C cc=new C();
		//对象实现一个接口,用这个对象和这个接口进行instanceof判断,都为true。
		System.out.println("ab instanceof A="+(ab instanceof A));
		System.out.println("ac instanceof A="+(ac instanceof A));
		System.out.println("bc instanceof A="+(bc instanceof A));
		System.out.println("bb instanceof A="+(bb instanceof A));
		System.out.println("cc instanceof A="+(cc instanceof A));
		//对象和父类进行instanceof判断,都为true
		System.out.println("ab instanceof B="+(ab instanceof B));
		System.out.println("ac instanceof B="+(ac instanceof B));
		System.out.println("bc instanceof B="+(bc instanceof B));
		System.out.println("bb instanceof B="+(bb instanceof B));
		System.out.println("cc instanceof B="+(cc instanceof B));
		//对象和他的子类进行instanceof判断为false
		System.out.println("ab instanceof C="+(ab instanceof C));
		System.out.println("ac instanceof C="+(ac instanceof C));
		System.out.println("bc instanceof C="+(bc instanceof C));
		System.out.println("bb instanceof C="+(bb instanceof C));
		System.out.println("cc instanceof C="+(cc instanceof C));
	}
}


程序输出:
ab instanceof A=true
ac instanceof A=true
bc instanceof A=true
bb instanceof A=true
cc instanceof A=true
ab instanceof B=true
ac instanceof B=true
bc instanceof B=true
bb instanceof B=true
cc instanceof B=true
ab instanceof C=false
ac instanceof C=true
bc instanceof C=true
bb instanceof C=false
cc instanceof C=true

 

你可能感兴趣的:(JavaSe)