摘要:多态中的引用类型转换代码自动类型提升,向上类型转换向下类型转换强制类型转型无法进行类型转换
多态中的引用类型转换 代码
</>复制代码
/javaDemo4/src/com/imooc/Animal.java
</>复制代码
package com.imooc;
public class Animal {
}
</>复制代码
/javaDemo4/src/com/imooc/Dog.java
</>复制代码
package com.imooc;
public class Dog extends Animal {
}
</>复制代码
/javaDemo4/src/com/imooc/Cat.java
</>复制代码
package com.imooc;
public class Cat extends Animal {
}
</>复制代码
Initail.java
</>复制代码
package com.imooc;
public class Initail {
public static void main(String[] args) {
Dog dog = new Dog();
Animal animal = dog; //自动类型提升,向上类型转换
Dog dog2 = (Dog)animal; //向下类型转换 强制类型转型
if(animal instanceof Cat) {
Cat cat = (Cat)animal;
}else {
System.out.println("无法进行类型转换");
}
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/73145.html
摘要:中的多态引用的多态父类的引用是可以指向子类对象报错方法多态动物具有吃的能力狗具有吃肉的能力狗具有看门的能力父类的引用是可以指向子类对象报错 java中的多态 showImg(https://segmentfault.com/img/bVbnDYA?w=1138&h=612); showImg(https://segmentfault.com/img/bVbnD6B?w=344&h=17...
摘要:中的接口实例智能手机和都有玩游戏的功能代码具有玩游玩的功能具有玩游玩的功能 java中的接口 showImg(https://segmentfault.com/img/bVbnEzV?w=1194&h=621); showImg(https://segmentfault.com/img/bVbnEzX?w=1192&h=615); showImg(https://segmentfaul...
摘要:中的抽象类代码实现通过键盘来打电话通过键盘来打短信通过语音来打电话通过语音来发短信运行 java中的抽象类 showImg(https://segmentfault.com/img/bVbnEiG?w=1189&h=613); showImg(https://segmentfault.com/img/bVbnEiH?w=1214&h=620); showImg(https://segm...
摘要:中的继承初始化顺序父类和子类年龄动物可以吃东西类执行了年龄狗可以吃东西类执行了对象的属性和构造方法年龄动物可以吃东西类执行了的 java中的继承初始化顺序 showImg(https://segmentfault.com/img/bVbnBI1?w=1277&h=671); showImg(https://segmentfault.com/img/bVbnBKG?w=811&h=427...
java中的方法重写 showImg(https://segmentfault.com/img/bVbnBEj?w=1260&h=668); 重写前 showImg(https://segmentfault.com/img/bVbnBEu?w=347&h=158); /javaDemo3/src/com/imooc/Animal.java package com.imooc; public cl...
阅读 3480·2023-04-26 00:39
阅读 4104·2021-09-22 10:02
阅读 2568·2021-08-09 13:46
阅读 1113·2019-08-29 18:40
阅读 1462·2019-08-29 18:33
阅读 787·2019-08-29 17:14
阅读 1533·2019-08-29 12:40
阅读 2989·2019-08-28 18:07
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要