This question has been asked in a C++ context但我对Java很好奇。关于虚拟方法的担忧不适用(我认为),但如果您遇到这种情况:
abstract class Pet
{
private String name;
public Pet setName(String name) { this.name = name; return this; }
}
class Cat extends Pet
{
public Cat catchMice() {
System.out.println("I caught a mouse!");
return this;
}
}
class Dog extends Pet
{
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!");
return this;
}
}
class Bird extends Pet
{
public Bird layEgg() {
...
return this;
}
}
{
Cat c = new Cat();
c.setName("Morris").catchMice(); // error! setName returns Pet, not Cat
Dog d = new Dog();
d.setName("Snoopy").catchFrisbee(); // error! setName returns Pet, not Dog
Bird b = new Bird();
b.setName("Tweety").layEgg(); // error! setName returns Pet, not Bird
}
在这种类层次结构中,有没有办法以不(有效)向上转换对象类型的方式返回this
?
答案 0 :(得分:53)
如果你想避免编译器发送未经检查的强制警告(并且不想@SuppressWarnings(“未选中”)),那么你需要做更多的事情:
首先,你对Pet的定义必须是自引用的,因为Pet总是一般类型:
abstract class Pet <T extends Pet<T>>
其次,setName中的(T) this
强制转换也未选中。为避免这种情况,请在优秀的Generics FAQ by Angelika Langer:
“getThis”技巧提供了一种方法 恢复这个的确切类型 参考
这导致下面的代码编译并在没有警告的情况下运行。如果你想扩展你的子类,那么这个技术仍然存在(尽管你可能需要对你的中间类进行泛化)。
结果代码是:
public class TestClass {
static abstract class Pet <T extends Pet<T>> {
private String name;
protected abstract T getThis();
public T setName(String name) {
this.name = name;
return getThis(); }
}
static class Cat extends Pet<Cat> {
@Override protected Cat getThis() { return this; }
public Cat catchMice() {
System.out.println("I caught a mouse!");
return getThis();
}
}
static class Dog extends Pet<Dog> {
@Override protected Dog getThis() { return this; }
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!");
return getThis();
}
}
public static void main(String[] args) {
Cat c = new Cat();
c.setName("Morris").catchMice();
Dog d = new Dog();
d.setName("Snoopy").catchFrisbee();
}
}
答案 1 :(得分:19)
这个老把戏怎么样:
abstract class Pet<T extends Pet>
{
private String name;
public T setName(String name) { this.name = name; return (T) this; }
}
class Cat extends Pet<Cat>
{
/* ... */
}
class Dog extends Pet<Dog>
{
/* ... */
}
答案 2 :(得分:10)
不,不是真的。您可以使用协变返回类型来解决它(感谢McDowell提供正确的名称):
@Override
public Cat setName(String name) {
super.setName(name);
return this;
}
(协变返回类型仅适用于Java 5及更高版本,如果您对此感兴趣的话。)
答案 3 :(得分:5)
这有点令人费解,但你可以用泛型来做到这一点:
abstract class Pet< T extends Pet > {
private String name;
public T setName( String name ) {
this.name = name;
return (T)this;
}
public static class Cat extends Pet< Cat > {
public Cat catchMice() {
System.out.println( "I caught a mouse!" );
return this;
}
}
public static class Dog extends Pet< Dog > {
public Dog catchFrisbee() {
System.out.println( "I caught a frisbee!" );
return this;
}
}
public static void main (String[] args){
Cat c = new Cat();
c.setName( "Morris" ).catchMice(); // error! setName returns Pet, not Cat
Dog d = new Dog();
d.setName( "Snoopy" ).catchFrisbee(); // error! setName returns Pet, not Dog
}
}
答案 4 :(得分:3)
public class Pet<AnimalType extends Pet> {
private String name;
public AnimalType setName(String name) {
this.name = name; return (AnimalType)this;
}
}
和
public class Cat extends Pet<Cat> {
public Cat catchMice() {return this;}
public static void main(String[] args) {
Cat c = new Cat().setName("bob").catchMice();
}
}