java中instanceof与Class的等价性代码示例
本文研究的主要是java中instanceof与Class的等价性的相关问题,具体如下。
java中的instanceof运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
实例1(instanceof)
接口Person
publicinterfacePerson{ publicvoideat(); }
实现类People
publicclassPeopleimplementsPerson{ privateinta=0; @Override publicvoideat(){ System.out.println("======"+a); } }
子类xiaoming:
publicclassxiaomingextendsPeople{ privateStringname; @Override publicvoideat(){ System.out.println("+++++++++"); } }
主函数
publicstaticvoidmain(String[]args){ Peoplep=newPeople(); xiaomingx=newxiaoming(); System.out.println(pinstanceofPerson); System.out.println(pinstanceofxiaoming);-----2 System.out.println(xinstanceofPerson); System.out.println(xinstanceofPeople); }
注意:上面2处的代码在编译时不会报错。
运行结果:
true false true true
实例2
packagecom.test.class_obj; classBase{ } classDerivedextendsBase{ } publicclassFamilyVsExactType{ staticvoidtest(Objectx){ System.out.println("Testingxoftype"+x.getClass().getSimpleName()); System.out.println("-----------------------------------------"); System.out.println("xinstanceofBase"+(xinstanceofBase)); System.out.println("xinstanceofDerived"+(xinstanceofDerived)); 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("*****************************************"); } publicstaticvoidmain(String[]args){ test(newBase()); test(newDerived()); } }
输出内容如下:
TestingxoftypeBase ----------------------------------------- xinstanceofBasetrue xinstanceofDerivedfalse ----------------------------------------- Base.isInstance(x)true Derived.isInstance(x)false ----------------------------------------- x.getClass()==Base.classtrue x.getClass()==Derived.classfalse x.getClass().equals(Base.class))true x.getClass().equals(Derived.class))false ***************************************** ***************************************** TestingxoftypeDerived ----------------------------------------- xinstanceofBasetrue xinstanceofDerivedtrue ----------------------------------------- Base.isInstance(x)true Derived.isInstance(x)true ----------------------------------------- x.getClass()==Base.classfalse x.getClass()==Derived.classtrue x.getClass().equals(Base.class))false x.getClass().equals(Derived.class))true ***************************************** ***************************************** Processfinishedwithexitcode0
通过以上测试可以得出以下结论:
- instanceof()和isInstance()生成的结果相同
- equals()和==生成的结果相同
- 父类可以是子类的实例,但子类不可以是父类的实例
- Class对象比较时,不考虑继承
总结
以上就是本文关于java中instanceof与Class的等价性代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!