The Type Comparison Operator instanceof
2011-07-20 12:15
399 查看
The
particular interface.
The following program,
a simple interface (named
Output:
When using the
keep in mind that
not an instance of anything.
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: true
When using the
instanceofoperator,
keep in mind that
nullis
not an instance of anything.
相关文章推荐
- The Type Comparison Operator instanceof
- Here is the order of operation for constructing the first instance of a type.
- No enclosing instance of type TextRunnable is accessible. Must qualify the allocation with an enclos
- No enclosing instance of type A is accessible. Must qualify the allocation with an enclosin
- Java 程序报错“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclo”
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java编译时出现 No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh
- java编译错误No enclosing instance of type TestFrame is accessible. Must qualify the allocation with an enclosing instance of type TestFrame (e.g. x.new A(
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type
- 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
- Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x
- No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing inst
- Java出现No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosin
- No enclosing instance of type B is accessible. Must qualify the allocation with an enclosing ins (20
- [转] EF cannot be tracked because another instance of this type with the same key is already being tracked
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type X 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