Java中instanceof关键字的用法总结

时间:2022-12-11 15:52:27 

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

用法:
result = object instanceof class
参数:
Result:布尔类型。
Object:必选项。任意对象表达式。
Class:必选项。任意已定义的对象类。
说明:
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。

例子如下:


package com.instanceoftest;
interface A{}
class B implements A{

}
class C extends B {

}

class instanceoftest {
public static void main(String[] args){
A a=null;
B b=null;
boolean res;

System.out.println("instanceoftest test case 1: ------------------");
res = a instanceof A;
System.out.println("a instanceof A: " + res);

res = b instanceof B;
System.out.println("b instanceof B: " + res);

System.out.println("\ninstanceoftest test case 2: ------------------");
a=new B();
b=new B();

res = a instanceof A;
System.out.println("a instanceof A: " + res);

res = a instanceof B;
System.out.println("a instanceof B: " + res);
res = b instanceof A;
System.out.println("b instanceof A: " + res);

res = b instanceof B;
System.out.println("b instanceof B: " + res);

System.out.println("\ninstanceoftest test case 3: ------------------");
B b2=(C)new C();

res = b2 instanceof A;
System.out.println("b2 instanceof A: " + res);

res = b2 instanceof B;
System.out.println("b2 instanceof B: " + res);

res = b2 instanceof C;
System.out.println("b2 instanceof C: " + res);
}
}

/*
result:

instanceoftest test case 1: ------------------
a instanceof A: false
b instanceof B: false
instanceoftest test case 2: ------------------
a instanceof A: true
a instanceof B: true
b instanceof A: true
b instanceof B: true
instanceoftest test case 3: ------------------
b2 instanceof A: true
b2 instanceof B: true
b2 instanceof C: true



instanceof是Java的一个二元操作符,和==,>,<是同一类东东。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据。

用法: 某个实例对象      instanceof      某个类名

instanceof 通常用于根据不同的实例调用不同的方法:

一、在有继承关系的类中我们可以通过多态来调用不同实例中的不同方法:

例1:

  有三个类,类名以及它们之间的关系如下

   Animal (Superclass)     Dog(Subclass)     Cat(Subclass)

   则可得出如下对象

   Animal  animal =new Animal (); ====》animal  instanceof Animal    返回 true

   Dog   dog=new  Dog();====》dog  instanceof  Dog    返回 true

   Cat    cat=new  Cat();====》cat  instanceof  Cat    返回  true

   Animal  dog=new  Dog();====》dog  instanceof  Animal    返回 true

   Animal  cat=new  Cat();====》cat  instanceof  Animal    返回 true


  Animal dog=new Dog();
  Animal cat=new Cat();

  List list = new ArrayList();

  list.add(dog);
  list.add(cat);

  Iterator it = list.iterator();
  while (it.hasNext()) {
     it.next().animalDo();

  }

在这里我们可以在Dog与Cat类中重写Animal中的animalDo方法,通过调用animalDo方法,然后会自动根据不同的实例调用不同类中的方法.

二、在没有继承关系的类中,我们可以通过instanceof来判断当前实例,然后很据不同实例调用不同方法:

例2: 


  Station s = new Station();
  Cell c = new Cell();


  List list = new ArrayList();

  list.add(s);
  list.add(c);


  Iterator it = list.iterator();
  while (it.hasNext()) {
   Object obj = it.next();
   if (obj instanceof Station ) {
    Station s1 = (Station ) obj;
    s1.stationDo();
   }
   if (obj instanceof Cell ) {
    Cell c1 = (Cell ) obj;
    c1.cellDo();
   }
  }

 在这里我们可以通过instanceof 判断结果,执行不同类中的相应动作方法(stationDo()、cellDo())。

一般在使用无泛型的集合(List、set等)时,比较多的使用  instanceof  ,由于集合能够存各种对象,所以在读取时一般要进行相应的判断。

标签:Java,instanceof
0
投稿

猜你喜欢

  • 使用Maven配置Spring的方法步骤

    2023-02-05 18:37:46
  • 使用Thumbnails实现图片指定大小压缩

    2021-11-14 11:07:34
  • 1秒实现Springboot 图片添加水印功能

    2022-07-21 04:30:59
  • 全面解析java final关键字

    2023-03-16 03:04:55
  • Android判断网络类型的方法(2g,3g还是wifi)

    2023-09-15 10:53:23
  • javaweb文件打包批量下载代码

    2022-10-24 17:10:17
  • Java多线程-线程的同步与锁的问题

    2023-11-29 01:40:12
  • jvm虚拟机类加载机制详解

    2021-10-21 08:33:28
  • C# 构造函数如何调用虚方法

    2023-05-12 00:08:57
  • java如何测试网络连通性

    2023-04-05 01:32:36
  • C#实现基于任务的异步编程模式

    2023-01-08 19:21:15
  • MyBatis Example And与Or混合使用的实例

    2021-11-11 01:53:13
  • mybatis-plus Wrapper条件构造器updateForSet更新方式

    2022-12-30 10:40:19
  • 基于C#技术实现身份证识别功能

    2023-10-01 14:16:26
  • C#使用Dispose模式实现手动对资源的释放

    2022-09-21 16:12:14
  • Android SurfaceView拍照录像实现方法

    2022-06-18 17:07:20
  • C语言 auto和register关键字

    2021-11-03 02:40:54
  • C#多线程之Thread类详解

    2023-07-23 05:34:46
  • Java多线程Atomic包操作原子变量与原子类详解

    2023-08-18 07:46:25
  • Java 方法的定义与调用详解

    2023-11-04 13:52:58
  • asp之家 软件编程 m.aspxhome.com