39

这个问题是在 C++ 上下文中提出的,但我对 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不(有效)向上转换对象类型的方式返回?

4

5 回答 5

58

如果您想避免来自编译器的未经检查的强制转换警告(并且不想 @SuppressWarnings("unchecked")),那么您需要做更多的事情:

首先,您对 Pet 的定义必须是自引用的,因为 Pet 始终是泛型类型:

abstract class Pet <T extends Pet<T>>

其次,(T) thissetName 中的强制转换也未选中。为避免这种情况,请使用Angelika Langer 出色的泛型常见问题解答中的“getThis”技术:

“getThis”技巧提供了一种恢复 this 引用的确切类型的方法。

这导致下面的代码,它编译并运行没有警告。如果您想扩展您的子类,那么该技术仍然适用(尽管您可能需要泛化您的中间类)。

结果代码是:

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();
  }
}
于 2009-07-01T18:07:38.343 回答
20

这个老把戏怎么样:

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>
{
    /* ... */
}
于 2009-07-01T14:55:46.783 回答
12

不,不是。您可以通过使用协变返回类型来解决它(感谢 McDowell 的正确名称):

@Override
public Cat setName(String name) {
    super.setName(name);
    return this;
}

(协变返回类型仅适用于 Java 5 及更高版本,如果您担心的话。)

于 2009-07-01T14:52:01.580 回答
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
    }

}
于 2009-07-01T14:56:43.740 回答
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();
    }

}

于 2009-07-01T15:05:46.077 回答