Fork me on GitHub

instanceof关键字

-------------siwuxie095

   

   

   

   

   

instanceof 关键字:

   

在Java中可以使用 instanceof 关键字判断一个对象到底是不是一个类的实例,

从而来调用这个类的属性和方法

   

   

代码:

   

package com.siwuxie095.pol;

   

class A{

 

}

   

class B extends A{

 

}

   

public class PolDemo01 {

   

public static void main(String[] args) {

 

//instanceof 的返回值是布尔值

A a=new A();

System.out.println(a instanceof A);

System.out.println(a instanceof B);

 

System.out.println();

 

//向上转型后,A B 发生关联

A ax=new B();

System.out.println(ax instanceof A);

System.out.println(ax instanceof B);

}

   

}

   

   

   

运行一览:

   

   

   

   

   

   

【made by siwuxie095】

posted on 2017-03-18 17:17  siwuxie095  阅读(145)  评论(0编辑  收藏  举报

导航