亲宝软件园·资讯

展开

java中instanceof与Class的等价性 java中instanceof与Class的等价性代码示例

HeatDeath 人气:4
想了解java中instanceof与Class的等价性代码示例的相关内容吗,HeatDeath在本文为您仔细讲解java中instanceof与Class的等价性的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:class,instanceof,java,instanceof用法,java中的instanceof,instanceof和getclass,下面大家一起来学习吧。

本文研究的主要是java中instanceof与Class的等价性的相关问题,具体如下。

java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。

实例1(instanceof)

接口Person

public interface Person {
public void eat();
}

实现类People

public class People implements Person {
private int a=0;
 @Override
 public void eat() {
 System.out.println("======"+a);
 
 }

}

子类xiaoming:

public class xiaoming extends People {
private String name;

@Override
public void eat() {
 System.out.println("+++++++++");
}
}

主函数

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处的代码在编译时不会报错。

运行结果:

true
false
true
true

实例2

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());
	}
}

输出内容如下:

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

通过以上测试可以得出以下结论:

总结

以上就是本文关于java中instanceof与Class的等价性代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论