本文研究的主要是java中instanceof与Class的等价性的相关问题,具体如下。
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
实例1(instanceof)
接口Person
1
2
3
|
public interface Person {
public void eat();
}
|
实现类People
1
2
3
4
5
6
7
8
9
|
public class People implements Person {
private int a= 0 ;
@Override
public void eat() {
System.out.println( "======" +a);
}
}
|
子类xiaoming:
1
2
3
4
5
6
7
8
|
public class xiaoming extends People {
private String name;
@Override
public void eat() {
System.out.println( "+++++++++" );
}
}
|
主函数
1
2
3
4
5
6
7
8
|
public static void main(String[] args) {
People p= new People();
xiaoming x= new xiaoming();
System.out.println(p instanceof Person);
System.out.println(p instanceof xiaoming); ----- 2
System.out.println(x instanceof Person);
System.out.println(x instanceof People);
}
|
注意:上面2处的代码在编译时不会报错。
运行结果:
1
2
3
4
|
true
false
true
true
|
实例2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.test.class_obj;
class Base {
}
class Derived extends Base {
}
public class FamilyVsExactType {
static void test(Object x) {
System.out.println( "Testing x of type " + x.getClass().getSimpleName());
System.out.println( "-----------------------------------------" );
System.out.println( "x instanceof Base " + (x instanceof Base));
System.out.println( "x instanceof Derived " + (x instanceof Derived));
System.out.println( "-----------------------------------------" );
System.out.println( "Base.isInstance(x) " + Base. class .isInstance(x));
System.out.println( "Derived.isInstance(x) " +
Derived. class .isInstance(x));
System.out.println( "-----------------------------------------" );
System.out.println( "x.getClass() == Base.class " +
(x.getClass() == Base. class ));
System.out.println( "x.getClass() == Derived.class " +
(x.getClass() == Derived. class ));
System.out.println( "x.getClass().equals(Base.class)) " +
(x.getClass().equals(Base. class )));
System.out.println( "x.getClass().equals(Derived.class)) " +
(x.getClass().equals(Derived. class )));
System.out.println( "*****************************************" );
System.out.println( "*****************************************" );
}
public static void main(String[] args) {
test( new Base());
test( new Derived());
}
}
|
输出内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
Testing x of type Base
-----------------------------------------
x instanceof Base true
x instanceof Derived false
-----------------------------------------
Base.isInstance(x) true
Derived.isInstance(x) false
-----------------------------------------
x.getClass() == Base. class true
x.getClass() == Derived. class false
x.getClass().equals(Base. class )) true
x.getClass().equals(Derived. class )) false
*****************************************
*****************************************
Testing x of type Derived
-----------------------------------------
x instanceof Base true
x instanceof Derived true
-----------------------------------------
Base.isInstance(x) true
Derived.isInstance(x) true
-----------------------------------------
x.getClass() == Base. class false
x.getClass() == Derived. class true
x.getClass().equals(Base. class )) false
x.getClass().equals(Derived. class )) true
*****************************************
*****************************************
Process finished with exit code 0
|
通过以上测试可以得出以下结论:
- instanceof() 和 isInstance() 生成的结果相同
- equals() 和 == 生成的结果相同
- 父类可以是子类的实例,但子类不可以是父类的实例
- Class 对象比较时,不考虑继承
总结
以上就是本文关于java中instanceof与Class的等价性代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/heatdeath/article/details/79099861