这个问题是在 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 以一种不会(有效)向上转换对象类型的方式?

有帮助吗?

解决方案

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

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

abstract class Pet <T extends Pet<T>>

其次, (T) this setName 中的强制转换也未选中。为了避免这种情况,请使用优秀的“getThis”技术 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();
  }
}

其他提示

这个老把戏怎么样:

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>
{
    /* ... */
}

不,不是真的。您可以通过使用协变返回类型来解决这个问题(感谢 McDowell 提供了正确的名称):

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

(协变返回类型仅在 Java 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
    }

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

}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top