协变返回类型
2012-02-16 20:20
155 查看
协变返回类型:子类方法重写父类方法,但返回类型是相应父类方法 返回值的子类(接口也行)。
class Something {
}
class Bone extends Something{
}
class Animal {
public Something eat() {
System.out.println("eat something");
return new Something();
}
}
class Dog extends Animal {
public Bone eat() {
System.out.println("eat bone");
return new Bone();
}
}
public class CovariantReturn {
public static void main(String[] args) {
Animal m = new Animal();
Dog d = new Dog();
m.eat();
d.eat();
}
}
class Something {
}
class Bone extends Something{
}
class Animal {
public Something eat() {
System.out.println("eat something");
return new Something();
}
}
class Dog extends Animal {
public Bone eat() {
System.out.println("eat bone");
return new Bone();
}
}
public class CovariantReturn {
public static void main(String[] args) {
Animal m = new Animal();
Dog d = new Dog();
m.eat();
d.eat();
}
}
相关文章推荐
- java协变返回类型使用示例
- C++ 返回类型协变
- java学习笔记---协变返回类型
- Java之协变返回类型详解(附源码)
- Java协变返回类型
- 【JavaSE】协变返回类型
- 返回类型协变
- Java | 协变返回类型学习时的一个例子
- Covariant Returen Types(协变返回类型)
- 协变返回类型
- 协变、逆变与不变:数组、泛型、与返回类型
- java泛型中桥方法以及协变返回类型
- 理解Java中的协变返回类型
- 新内容:协变返回类型
- LearnJava 多态之协变返回类型
- C++动态绑定及返回类型协变
- 函数重写(override)与协变返回类型(covariant return type)
- 协变返回类型
- 协变返回类型
- 编程思想 之「多态、初始化顺序、协变返回类型」