您的位置:首页 > 其它

协变返回类型

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: