教妹学Java(三十八):instanceof 操作符的用法

System.out.println(simple instanceof Simple);

}

}

在上面这个例子中,我们使用 instanceof 判断 simple 这个引用变量是不是 Simple 类。输出结果如下所示:

true

一个子类的对象既是子类也是父类,来看下面这个示例:

/**

  • @author 微信搜「沉默王二」,回复关键字 PDF

*/

class Animal {}

public class Dog extends Animal{

public static void main(String[] args) {

Dog dog = new Dog();

System.out.println(dog instanceof Dog);

System.out.println(dog instanceof Animal);

}

}

程序输出结果如下所示:

true

true

当子类变量指向的是父类对象,这被称为“向下转型”。如果我们直接转的话,编译器将会报错。

教妹学Java(三十八):instanceof 操作符的用法_第1张图片

如果强制向下转型的话,编译器不会报错,但运行是会报错。

class Animal {}

public class Dog extends Animal{

public static void main(String[] args) {

Dog dog1 = (Dog)new Animal();

}

}

运行时抛出 ClassCastException 异常:

Exception in thread “main” java.lang.ClassCastException: class com.itwanger.thirtyeight.Animal cannot be cast to class com.itwanger.thirtyeight.Dog (com.itwanger.thirtyeight.Animal and com.itwanger.thirtyeight.Dog are in unnamed module of loader ‘app’)

at com.itwanger.thirtyeight.Dog.main(Dog.java:9)

来看一下 instanceof 操作符的具体使用场景:

/**

  • @author 微信搜「沉默王二」,回复关键字 PDF

*/

public class Test {

public static void main(String[] args) {

I i = new B();

Call call = new Call();

call.invoke(i);

}

}

interface I{}

class A implements I {

public void a() {

System.out.println(“a”);

}

}

class B implements I {

public void b() {

System.out.println(“b”);

}

}

class Call {

void invoke(I i) {

if (i instanceof A) {

A a = (A)i;

a.a();

}

if (i instanceof B) {

B b = (B)i;

b.b();

}

}

}

在上面的例子中,类 A 和类 B 都实现了接口 I, A 有一个 a() 方法,B 有一个 b() 方法,在 Call 类中,有一个 invoke() 方法,它的参数类型为 I,在方法体重,通过 instanceof 操作符判断参数 i 到底是 A 还是 B,如果是 A,对 i 向下转型为 A,然后调用 a() 方法,如果是 B ,对 i 向下转型为 B,然后调用 b() 方法。

来看一下程序的输出结果:

b

因为引用类型 i 指向的是对象 B。

“三妹,instanceof 操作符我们就学到这里吧,它的用法我相信你一定全部掌握了。”我揉一揉犯困的双眼,疲惫地给三妹说。

“好的,二哥,我这就去练习去。”三妹似乎意犹未尽,这种学习状态真令我感到开心。

二哥肝了两天两夜,《程序员不可或缺的软实力》第一版强势来袭,纯手敲,足足 20 万字精华文章,贯穿了我十余年的编程生涯,涉及到了生活和工作中的方方面面,如果你是迷茫的在校大学生,或者刚入职的新人,相信我的个人经历,可以给你带去一些思考,从而树立起正确的人生观和价值观。

教妹学Java(三十八):instanceof 操作符的用法_第2张图片

你可能感兴趣的:(java,python,开发语言)