java instanceof 关键字

来源:互联网 发布:我们如何看待网络用语 编辑:程序博客网 时间:2024/06/07 02:47

java 中使用instanceof 关键字判断一个对象到底是否是不是一个类的实例(判断返回值 是 布尔类型值)

案例代码 如下:

package csdn.zyl.demo;
class A12{
 public void tell1()
 {
  System.out.println("A12--tell1");
 }
 public void tell2()
 {
  System.out.println("A12--tell2");
 }
}
class B1 extends A12{
 public void tell1(){
  System.out.println("B1--tell1");
 }
 public void tell3(){
  System.out.println("B1--tell3");
 }
}
public class DuoTaiDemo {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
      //向上转型
//  System.out.println("向上转型结果");
//  B1 b = new B1();
//  A12 a = b;
//  a.tell1(); //tell1 ()重写
//  a.tell2();
//  //向下 转型
//  System.out.println("向下转型结果");
  A12 a1 = new B1();
  B1 b1 = (B1)a1;
  System.out.println(a1 instanceof B1);
  System.out.println(b1 instanceof A12);
  
 }
}
案例 结果
true
true


原创粉丝点击