instanceof
2021/10/22 23:09:58
本文主要是介绍instanceof,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
instanceof
public class Person { public void run(){ System.out.println("run"); } }
public class Stendyent extends Person{ public void go(){ System.out.println("go"); } }
public class Teacher extends Person{ }
public class AppLication { public static void main(String[] args) { //类型之间的转换:父(高) 子(低) Person s = new Stendyent(); //将s对象强制转换为Stendyent类型就可以使用go方法,强制转换可能会丢失一些自己本来的方法 ((Stendyent) s).go(); /*Object object = new Stendyent(); //System.out.println(x instanceof y);能不能编译通过主要看两者之间有没有父子关系 //Object > Person > Stuendy System.out.println(object instanceof Stendyent);//true System.out.println(object instanceof Person);//true System.out.println(object instanceof Object);//true //Object > Person > Teacher System.out.println(object instanceof Teacher);//false //Object > String System.out.println(object instanceof String);//false System.out.println("=================="); Person person = new Stendyent(); System.out.println(person instanceof Stendyent);//true System.out.println(person instanceof Person);//true System.out.println(person instanceof Object);//true System.out.println(person instanceof Teacher);//false //System.out.println(person instanceof String);//编译器报错 System.out.println("=================="); Stendyent stendyent = new Stendyent(); System.out.println(stendyent instanceof Stendyent);//true System.out.println(stendyent instanceof Person);//true System.out.println(stendyent instanceof Object);//true //System.out.println(stendyent instanceof Teacher);//编译器报错 //System.out.println(stendyent instanceof String);//编译器报错 */ } }
这篇关于instanceof的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-29【机器学习(二)】分类和回归任务-决策树(Decision Tree,DT)算法-Sentosa_DSML社区版
- 2024-11-29怎么去掉UniApp中字符串的空格?-icode9专业技术文章分享
- 2024-11-29Warning: Cannot modify header information - headers already sent by 报错信息是什么?-icode9专业技术文章分享
- 2024-11-29Excel中实现拖动排序的简易教程
- 2024-11-29如何在Excel中使用拖动排序功能
- 2024-11-28阿里云 ECS课程:新手入门教程
- 2024-11-27Excel中实现拖动排序的简单教程
- 2024-11-27Rocket消息队列资料:新手入门指南
- 2024-11-27rocket消息队资料详解与入门指南
- 2024-11-27RocketMQ底层原理资料详解入门教程