Java实例
时间:2020-02-23 14:36:17 来源:igfitidea点击:
instanceof java关键字用于检查对象是否为某种类型。
Java instanceof运算符的语法为" obj instanceOf Object"-左侧为实例,右侧为Class名称。
Java instanceof运算符返回布尔结果。
Java实例
在本节中,让我们看看如何使用instanceof关键字。
假设我们有一些如下定义的接口和类。
interface Vehicle { }
class Car { }
class Ferrari extends Car implements Vehicle {}
如果我们检查上述类对象的instanceof关键字,我们将看到以下结果:
Ferrari ferrari = new Ferrari(); ferrari instanceof Vehicle //true - Ferrari implements Vehicle ferrari instanceof Car //true - Ferrari extends Car ferrari instanceof Ferrari //true - Ferrari is Ferrari ferrari instanceof Object //true - Object is the parent type of all objects
但是,如果我们扭转某些事情,我们将看到不同的结果:
Car car = new Car(); car instanceof Ferrari //false - Car is super type of Ferrari, not other way around
当您有很多对象并且不确定它们是什么时," instanceof"是一个有用的工具。
instanceof运算符vs isInstance()方法
基本上,两者都做相同的事情,请检查一个对象是否与另一个对象属于同一类的实例。
instanceof运算符在编译时起作用,而isInstance()方法在运行时起作用。
例如:
//This method tells us whether the object is an
//instance of class whose name is passed as a
//string 'c'.
public static boolean checkType(Object obj, String c)
throws ClassNotFoundException {
return Class.forName(c).isInstance(obj);
}
让我们使用上面的方法来演示其用法:
String str = "Shubham"; checkType(str, "java.lang.String"); //true checkType(str, "java.lang.Object"); //true
Java instanceof与数组
Java数组也是对象,但是instanceof运算符与数组一起使用时的工作方式有所不同。
例如;
new int[] {1} instanceof Object; //true
但是,如果我们尝试以下操作:
new String[]{"1"} instanceof String
然后,由于"不兼容的条件操作数类型String []和String",我们得到了编译时错误。
但是下面使用instanceof运算符可以正常工作。
new String[]{"1"} instanceof String[] //true
Java实例
这是instanceofjava运算符和isInstance方法的完整示例。
package com.theitroad.comstanceofoperator;
interface Vehicle {}
class Car {}
class Ferrari extends Car implements Vehicle {}
public class InstanceOfExample {
public static void main(String[] args) throws ClassNotFoundException {
Car car = new Car();
Ferrari ferrari = new Ferrari();
String str = "hyman";
String[] strArray = { "1" };
System.out.println(ferrari instanceof Vehicle); //true - Ferrari implements Vehicle
System.out.println(ferrari instanceof Car); //true - Ferrari extends Car
System.out.println(ferrari instanceof Ferrari); //true - Ferrari is Ferrari
System.out.println(ferrari instanceof Object); //true - Object is the parent type of all objects
System.out.println(null instanceof Object); //false - null is not an Object
System.out.println(car instanceof Ferrari); //false - Car is supertype of Ferrari, not other way around
System.out.println(new int[] { 1 } instanceof Object); //true
System.out.println(strArray instanceof String[]); //true
System.out.println("*");
System.out.println(checkType(str, "java.lang.String")); //true
System.out.println(checkType(str, "java.lang.Object")); //true
System.out.println(checkType(str, "com.theitroad.comstanceofoperator.Vehicle")); //false
System.out.println(checkType(strArray, "java.lang.Object")); //true
System.out.println(checkType(strArray, "java.lang.String")); //false
}
public static boolean checkType(Object obj, String c) throws ClassNotFoundException {
return Class.forName(c).isInstance(obj);
}
}

