The Type Comparison Operator instanceof
2011-07-28 16:56
302 查看
This piece of writing is reprinted from the following URL:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
The
The following program,
http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
The
instanceofoperator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
The following program,
InstanceofDemo, defines a parent class (named
Parent), a simple interface (named
MyInterface), and a child class (named
Child) that inherits from the parent and implements the interface.
class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); } } class Parent{} class Child extends Parent implements MyInterface{} interface MyInterface{}Output:
obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceof MyInterface: trueWhen using the
instanceofoperator, keep in mind that
nullis not an instance of anything.
相关文章推荐
- The Type Comparison Operator instanceof
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing 问题解释
- JAVA中出现No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of the type RegisterActivity is accessible
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosi
- 错误提示:No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing insta
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing ins
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 当Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 使用内部类时提示No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- java编译时出现Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x