方法链接+继承不能很好地结合在一起?

Jas*_*n S 38 java inheritance method-chaining

这个问题已在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
}
Run Code Online (Sandbox Code Playgroud)

在这种类层次结构中,有没有办法以this不(有效)向上转换对象类型的方式返回?

pau*_*lcm 55

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

首先,你对Pet的定义必须是自引用的,因为Pet总是一般类型:

abstract class Pet <T extends Pet<T>>
Run Code Online (Sandbox Code Playgroud)

其次,(T) thissetName中的强制转换也是未选中的.为了避免这种情况,请使用Angelika Langer出色的Generics FAQ中的"getThis"技术:

"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();
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 但是当你有非抽象的、非最终的类在中间并且需要创建一个实例时,这会变得有点棘手。例如,假设您有一个 `static class Poodle extends Dog&lt;Poodle&gt;`,并将 `Dog` 更改为 `static class Dog&lt;T extends Dog&lt;T&gt;&gt; extends Pet&lt;T&gt;`。现在创建 `Dog` 的原始实例会很困难。 (3认同)
  • `class Snake扩展Pet <Cat> {@Override protected Cat getThis(){return new Cat();}}` (2认同)

Ras*_*ber 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>
{
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*ers 10

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

@Override
public Cat setName(String name) {
    super.setName(name);
    return this;
}
Run Code Online (Sandbox Code Playgroud)

(协变返回类型仅在Java 5及更高版本中,如果您担心这一点.)


Ste*_* B. 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
    }

}
Run Code Online (Sandbox Code Playgroud)