Java基础篇:如何使用instanceof

300 阅读3分钟

有时,在运行时间内知道对象类型是很有用的。例如,你有一个执行线程生成各种类型的对象,其他线程处理这些对象。

这种情况下,让处理线程在接受对象时知道每一个对象的类型是大有裨益的。另一种在运行时间内知道对象的类型是很有用的情形是强制类型转换。

Java中非法强制类型转换导致运行时错误。很多非法的强制类型转换在编译时发生。然而包括类层次结构的强制类型转换可能产生仅能在运行时间里被察觉的非法强制类型转换。

例如,一个名为A的父类能生成两个子类B和C。这样,在强制B对象转换为类型A或强制C对象转换为类型A都是合法的,但强制B对象转换为C对象(或相反)都是不合法的。

因为类型A的一个对象可以引用B或C。但是你怎么知道,在运行时,在强制转换为C之前哪类对象被引用?它可能是A,B或C的一个对象。如果它是B的对象,一个运行时异常被引发。Java 提供运行时运算符instanceof来解决这个问题。

instanceof运算符具有下面的一般形式:

object instanceof type

这里,object是类的实例,而type是类的类型。如果object是指定的类型或者可以被强制转换成指定类型,instanceof将它评估成true,若不是,则结果为false。这样,instanceof是你的程序获得对象运行时类型信息的方法。

下面的程序说明了instanceof的应用:

// Demonstrate instanceof operator. 
class A { 
 int i, j; 
} 
class B { 
 int i, j; 
} 
class C extends A { 
 int k; 
} 
class D extends A { 
 int k; 
} 
class InstanceOf { 
 public static void main(String args[]) { 
 A a = new A(); 
 B b = new B(); 
 C c = new C(); 
 D d = new D(); 
 if(a instanceof A) 
 System.out.println("a is instance of A"); 
 if(b instanceof B) 
 System.out.println("b is instance of B"); 
 if(c instanceof C) 
 System.out.println("c is instance of C"); 
 if(c instanceof A) 
 System.out.println("c can be cast to A"); 
 if(a instanceof C) 
 System.out.println("a can be cast to C"); 
 System.out.println(); 
 // compare types of derived types 
 A ob; 
 ob = d; // A reference to d 
 System.out.println("ob now refers to d"); 
 if(ob instanceof D) 
 System.out.println("ob is instance of D"); 
 System.out.println(); 
 ob = c; // A reference to c 
 System.out.println("ob now refers to c"); 
 if(ob instanceof D) 
 System.out.println("ob can be cast to D"); 
 else 
 System.out.println("ob cannot be cast to D"); 
 if(ob instanceof A) 
 System.out.println("ob can be cast to A"); 
 System.out.println(); 
 // all objects can be cast to Object 
 if(a instanceof Object) 
 System.out.println("a may be cast to Object"); 
 if(b instanceof Object) 
 System.out.println("b may be cast to Object"); 
 if(c instanceof Object) 
 System.out.println("c may be cast to Object"); 
 if(d instanceof Object) 
 System.out.println("d may be cast to Object"); 
 } 
}

程序输出如下:

a is instance of A 
b is instance of B 
c is instance of C 
c can be cast to A 
ob now refers to d 
ob is instance of D 
ob now refers to c 
ob cannot be cast to D 
ob can be cast to A 
a may be cast to Object 
b may be cast to Object 
c may be cast to Object 
d may be cast to Object

多数程序不需要instanceof运算符,因为,一般来说,你知道你正在使用的对象类型。

但是,在你编写对复杂类层次结构对象进行操作的通用程序时它是非常有用的。