isInstance_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > isInstance

isInstance

 2014/8/19 21:13:51  ywu  程序员俱乐部  我要评论(0)
  • 摘要:说明:本系列博客是本人在工作中遇到的一些问题的整理,其中有些资料来源网络博客,有些信息来自出版的书籍,掺杂一些个人的猜想及验证,总结,主要目的是方便知识的查看,并非纯原创。本系列博客会不断更新。原创不容易,支持原创。对于参考的一些其他博客,会尽量把博客地址列在博客的后面,以方便知识的查看。Class类的isInstance(Objectobj)作用与instanceof关键字一样,只不过使用instanceof关键字时,关键字左侧是被检测对象,右侧是类型
  • 标签:instance

说明:

系列博客是本人在工作中遇到的一些问题的整理,其中有些资料来源网络博客,有些信息来自出版的书籍,掺杂一些个人的猜想及验证,总结,主要目的是方便知识的查看,并非纯原创。本系列博客会不断更新。原创不容易,支持原创。对于参考的一些其他博客,会尽量把博客地址列在博客的后面,以方便知识的查看。

?

Class类的isInstance(Object obj)作用与instanceof关键字一样,只不过使用instanceof关键字时,关键字左侧是被检测对象,右侧是类型,isInstance方法的参数是被检测对象

?

类层次还是一样,使用ABCD,其定义参看上一篇博客。看下如下测试类:

/**

?* Created with IntelliJ IDEA.

?* User: yejunwu123@gmail.com

?* Date: 2014-08-17 20:56

?* Description:测试java中Object.class.isInstance()方法

?*/

public class IsInstanceTest {

??? public static void main(String[] args) {

??????? //创建对象

??????? A ab = new B();

??????? A ac = new C();

?

??????? B bb = new B();

??????? B bc = new C();

?

??????? C cc = new C();

?

??????? D dd = new D();

?

??????? //A.class.isInstance(..)

??????? System.out.println("A.class.isInstance(ab) = " + (A.class.isInstance(ab)));

??????? System.out.println("A.class.isInstance(ac) = " + (A.class.isInstance(ac)));

??????? System.out.println("A.class.isInstance(bb) = " + (A.class.isInstance(bb)));

??????? System.out.println("A.class.isInstance(bc) = " + (A.class.isInstance(bc)));

??????? System.out.println("A.class.isInstance(cc) = " + (A.class.isInstance(cc)));

??????? System.out.println("A.class.isInstance(dd) = " + (A.class.isInstance(dd)));

??????? System.out.println("A.class.isInstance(null) = " + (A.class.isInstance(null)));

??????? System.out.println("-------------------------------------");

??????? //B.class.isInstance(..)

??????? System.out.println("B.class.isInstance(ab) = " + (B.class.isInstance(ab)));

??????? System.out.println("B.class.isInstance(ac) = " + (B.class.isInstance(ac)));

??????? System.out.println("B.class.isInstance(bb) = " + (B.class.isInstance(bb)));

??????? System.out.println("B.class.isInstance(bc) = " + (B.class.isInstance(bc)));

??????? System.out.println("B.class.isInstance(cc) = " + (B.class.isInstance(cc)));

??????? System.out.println("B.class.isInstance(dd) = " + (B.class.isInstance(dd)));

??????? System.out.println("B.class.isInstance(null) = " + (B.class.isInstance(null)));

??????? System.out.println("-------------------------------------");

??????? //C.class.isInstance(..)

??????? System.out.println("C.class.isInstance(ab) = " + (C.class.isInstance(ab)));

??????? System.out.println("C.class.isInstance(ac) = " + (C.class.isInstance(ac)));

??????? System.out.println("C.class.isInstance(bb) = " + (C.class.isInstance(bb)));

??????? System.out.println("C.class.isInstance(bc) = " + (C.class.isInstance(bc)));

??????? System.out.println("C.class.isInstance(cc) = " + (C.class.isInstance(cc)));

??????? System.out.println("C.class.isInstance(dd) = " + (C.class.isInstance(dd)));

??????? System.out.println("C.class.isInstance(null) = " + (C.class.isInstance(null)));

??????? System.out.println("-------------------------------------");

??????? //D.class.isInstance(..)

??????? System.out.println("D.class.isInstance(ab) = " + (D.class.isInstance(ab)));

??????? System.out.println("D.class.isInstance(ac) = " + (D.class.isInstance(ac)));

??????? System.out.println("D.class.isInstance(bb) = " + (D.class.isInstance(bb)));

??????? System.out.println("D.class.isInstance(bc) = " + (D.class.isInstance(bc)));

??????? System.out.println("D.class.isInstance(cc) = " + (D.class.isInstance(cc)));

??????? System.out.println("D.class.isInstance(dd) = " + (D.class.isInstance(dd)));

??????? System.out.println("D.class.isInstance(null) = " + (D.class.isInstance(null)));

??? }

}

可以看出,即使类DABC不属于同一个继承层次,编译也会通过,instanceof不同。

以下是控制台运行结果:

A.class.isInstance(ab) = true

A.class.isInstance(ac) = true

A.class.isInstance(bb) = true

A.class.isInstance(bc) = true

A.class.isInstance(cc) = true

A.class.isInstance(dd) = false

A.class.isInstance(null) = false

-------------------------------------

B.class.isInstance(ab) = true

B.class.isInstance(ac) = true

B.class.isInstance(bb) = true

B.class.isInstance(bc) = true

B.class.isInstance(cc) = true

B.class.isInstance(dd) = false

B.class.isInstance(null) = false

-------------------------------------

C.class.isInstance(ab) = false

C.class.isInstance(ac) = true

C.class.isInstance(bb) = false

C.class.isInstance(bc) = true

C.class.isInstance(cc) = true

C.class.isInstance(dd) = false

C.class.isInstance(null) = false

-------------------------------------

D.class.isInstance(ab) = false

D.class.isInstance(ac) = false

D.class.isInstance(bb) = false

D.class.isInstance(bc) = false

D.class.isInstance(cc) = false

D.class.isInstance(dd) = true

D.class.isInstance(null) = false

?

不难看出Class类的isInstance(Object obj)作用与instanceof关键字一样。

发表评论
用户名: 匿名