InstanceOf强制类型转换

类型之间的转换 :父类 子类
高 低
Person student = new Student();

student将这个对象转换为Student类型,就可以使用student类型的方法了
((Student) student).go();


子类转换为父类 可能丢失自己的本来的一些方法!

student本来是Person类型的变量,无法使用Person类的子类Student的go方法
但是使用Student转换为Student类型后,student就转换为Student类型的变量
就可以使用go方法
posted @ 2022-03-07 17:53  SmallPepsi  阅读(65)  评论(0)    收藏  举报