Java语言 instanceof关键字

instanceof是Java、php的一个二元操作符(运算符),和==,>,<是同一类东西。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是判断其左边对象是否为其右边类的实例,返回boolean类型的数据。可以用来判断继承中的子类的实例是否为父类的实现。相当于c#中的is操作符。java中的instanceof运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。——百度百科


booleanresult = object instanceof class
例子:booleanres=“name”instanceof String; //res为true
等效于: Class对象的非静态方法isInstance(Objectobj)返回值是boolean
例子:booleanres = String.getClass.isInstance(“name”); //res为true
空引用  instanceof 空引用对应的类名; //返回的结果是null


用Java的伪代码来表现Java语言规范所描述的运行时语义:
// obj instanceof T
boolean result;
try {
   T temp = (T) obj; //checkcast
   result = true;
} catch (ClassCastException e) {
   result = false;
}
如果有表达式 obj instanceof T ,那么如果(T) obj 不抛ClassCastException 异常则该表达式值为 true ,否则值为 false 。


在同一个类加载器加载的前提下,使用其生成的对象,去比较才有意义。不同的类加载器加载的类生成对象,互相instanceof返回false。
  Java类加载器(英语:JavaClassloader)是Java运行时环境(JavaRuntime Environment)的一部分,负责动态加载Java类到Java虚拟机的内存空间中。类通常是按需加载,即第一次使用该类时才加载。
JVM有一条名为instanceof 的指令,而Java源码编译到Class文件时会把Java语言中的instanceof 运算符映射到JVM的instanceof 指令上。


示例:
在有继承关系的类中我们可以通过多态来调用不同实例中的不同方法
//EatFood类
public interface EatFood {
	void eat();
}

//Animal类
public class Animal implements EatFood{
	public void eat() {
	System.out.println("I dont't konw what to eat");
}
public void eat(Animal animal){
if(animal instanceof Dog){
System.out.println("Dog eat bone1");
}
else if(animal instanceof Cat){
System.out.println("Cat eat fish1");
}
}
public void eat(Dog dog){
System.out.println("Dog eat bone2");
}
public void eat(Cat cat){
System.out.println("Cat eat fish2");
}
}

//Dog 类
public class Dog extends Animal{}
//Cat 类
public class Cat extends Animal{}
//主类
public class Test {

public static void main(String[] args) {
<span style="white-space:pre">	</span>boolean f;
	EatFood eat=null;
	f= eat instanceof EatFood; //false
	System.out.println(“eat instanceof EatFood:”+f);

	eat=new Dog();
	f= eat instanceof EatFood; //true
	System.out.println("eat instanceof EatFood:"+f);
Dog dog=null;
f= dog instanceof EatFood; //false
System.out.println("dog instanceof EatFood:"+f);

Dog dog1=new Dog();
f=dog1 instanceof Dog; //true
System.out.println("dog1 instanceof Dog:"+f);

f=dog1 instanceof Animal; //true
System.out.println("dog1 instanceof Animal:"+f);

f=dog1 instanceof EatFood; //true
System.out.println("dog1 instanceof EatFood:"+f);
Animal dog2=new Dog();
f=dog2 instanceof Dog; //true
System.out.println("dog2 instanceof Dog:"+f);

f=dog2 instanceof Animal; //true
System.out.println("dog2 instanceof Animal:"+f);

f=dog2 instanceof EatFood; //true
System.out.println("dog2 instanceof EatFood:"+f);
Animal animal=new Animal();
f=animal instanceof Dog; //false
System.out.println("animal instanceof Dog:"+f);

f=animal instanceof Animal; //true
System.out.println("animal instanceof Animal:"+f);

f=animal instanceof EatFood; //true
System.out.println("animal instanceof EatFood:"+f);
Cat cat=new Cat();
animal.eat(); //调用无参方法,输出I dont't konw what to eat
animal.eat(dog1); //调用参数为Dog对象的方法,输出Dog eat bone2
animal.eat(cat); //调用参数为Cat对象的方法,输出Cat eat fish2
animal.eat(dog2); 
//调用参数为Animal对象的方法,输出Dog eat bone1
}
}

参数为Animal的方法通常被认为是没有好好利用面向对象中的多态性。该功能用方法重载完全可以实现,这是面向对象编程应有的做法,避免回到结构化编程模式。

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

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  ,由于集合能够存各种对象,所以在读取时一般要进行相应的判断。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值