多态(Polymorphism)的强制类型转换
来源:互联网 发布:学校开展网络安全教育 编辑:程序博客网 时间:2024/05/17 15:05
public class PolyTest2 {
public static void main(String[] args) {
/*
Animal a = new Dog();
Dog dog = (Dog) a; //向下转换:它指向什么样的对象就可以转换成什么样的引用)
dog.sing();
*/
/*
Animal b = new Cat();
Cat d = (Dog)b; //错误,dog的引用不能指向cat的对象。
*/
/*
Cat cat = new Cat();
Animal animal = cat; //正确,将子类型转换为父类型.
animal.sing();
*/
/*Animal animal = new Animal();
Cat cat = (Cat)animal; // 错误,将Animal的引用不能转换为Cat.
*/
}
}
class Animal {
public void sing() {
System.out.println("anilma is singing");
}
}
class Dog extends Animal {
public void sing() {
System.out.println("dog is singing");
}
}
class Cat extends Animal {
public void sing() {
System.out.println("cai is singing");
}
}
- 多态(Polymorphism)的强制类型转换
- 多态(Polymorphism)的强制类型转换2
- 强制的类型转换
- 强制类型的转换
- C++的强制类型转换(转载)
- 类型强制转换的危险
- 强制类型转换的问题
- 指针的强制类型转换
- java的强制类型转换
- 函数的强制类型转换
- C#的强制类型转换
- 指针的强制类型转换
- 指针的强制类型转换
- C++的强制类型转换
- java类型的强制转换
- 地址类型的强制转换
- C++的强制类型转换
- c++的强制类型转换
- 路由接路由
- 我眼中的VB.Net
- Linux终端设备驱动(二)
- 时间倒计时
- Linux终端设备驱动(三)
- 多态(Polymorphism)的强制类型转换
- 数据字典是什么
- Android控件继承图
- 初试—迷宫求解
- Linux终端设备驱动(五)
- 一次使用telnet登录虚拟机电脑的过程
- c#串口通信Develop
- poj 1017 贪心算法
- linux基础复习(7)串口应用开发