3

在 Typescript 中,有一个多态返回类型的概念thishttps://www.typescriptlang.org/docs/handbook/advanced-types.html#polymorphic-this-types 示例:

export abstract class Animal {
    private name: string;

    public setName(name: string): this {
        this.name = name;
        return this;
    }
}

export class Dog extends Animal {
    private breed: string;

    public setBreed(breed: string): this {
        this.breed = breed;
        return this;
    }
}

export class FluffyDog extends Dog {
    private fluffiness: number;
    public setFluffiness(fluffiness: number): this {
        this.fluffiness = fluffiness;
        return this;
    }
}

export class Main {
    constructor() {
        const dog: FluffyDog = new FluffyDog()
            .setName('Fluffy')
            .setFluffiness(10)
            .setBreed('Lab');
    }
}

Java中有什么等价的吗?我想出的最好的是:

public abstract class Animal<T extends Animal<T>> {
    private String name;
    public T setName(String name) {
        this.name = name;
        return (T)this;
    }
}

class Dog extends Animal<Dog> {
    private String breed;
    public Dog setBreed(String breed) {
        this.breed = breed;
        return this;
    }
}


class Main {
    static {
        Dog dog =  new Dog()
                .setName("Fluffy")
                .setBreed("Lab");
    }
}

或这个:

public abstract class Animal {
    private String name;
    public <T extends Animal> T setName(String name) {
        this.name = name;
        return (T)this;
    }
}

class Dog extends Animal {
    private String breed;
    public <T extends Dog> T setBreed(String breed) {
        this.breed = breed;
        return (T)this;
    }
}

class FluffyDog extends Dog {
    private Long fluffiness;
    public <T extends FluffyDog> T setFluffiness(Long fluffiness) {
        this.fluffiness = fluffiness;
        return (T)this;
    }
}


class Main {
    static {
        FluffyDog dog =  new FluffyDog()
                .<FluffyDog>setName("Fluffy")
                .setFluffiness(10L)
                .setBreed("Lab");
    }
}

第一个似乎只能被子类化一次。
第二个在某些情况下需要显式类型参数。
有没有办法在 Java 中返回多态 this ?

4

2 回答 2

3

另外两个选项:

  1. 由于 setter 方法的顺序无关紧要,因此在功能上以自下而上的顺序调用它们。如果您首先分配给变量,则不需要强制转换。

    final FluffyDog dog = new FluffyDog();
    dog.setFluffiness(10)
       .setBreed("Lab")
       .setName("Fluffy");
    
  2. 覆盖继承的 setter 方法以更改返回类型,super用于委托给实际实现:

    class Animal {
        private String name;
        public Animal setName(String name) {
            this.name = name;
            return this;
        }
    }
    
    class Dog extends Animal {
        private String breed;
        @Override
        public Dog setName(String name) {
            super.setName(name);
            return this;
        }
        public Dog setBreed(String breed) {
            this.breed = breed;
            return this;
        }
    }
    
    class FluffyDog extends Dog {
        private long fluffiness;
        @Override
        public FluffyDog setName(String name) {
            super.setName(name);
            return this;
        }
        @Override
        public FluffyDog setBreed(String breed) {
            super.setBreed(breed);
            return this;
        }
        public FluffyDog setFluffiness(long fluffiness) {
            this.fluffiness = fluffiness;
            return this;
        }
    }
    
    class Main {
        public static void main(String[] args) throws Exception {
            final FluffyDog dog = new FluffyDog()
                    .setName("Fluffy")
                    .setBreed("Lab")
                    .setFluffiness(10);
        }
    }
    
于 2018-10-30T17:26:17.180 回答
2

使用您的第一个替代方案,您仍然可以进行两个级别的继承。诚然,它的可读性和可理解性不是很高。不幸的是,泛型是 1.5 中添加的 Java 的补充,许多 Java Bean 模式并不适合这些类型的链式方法。(您会注意到大多数 setter 方法void在传统的 Java 类中返回)。

public abstract class Animal<T extends Animal<T>> {
    private String name;
    public T setName(String name) {
        this.name = name;
        return (T)this;
    }
}

public class Dog<T extends Dog<T>> extends Animal<T> {
    private String breed;
    public T setBreed(String breed) {
        this.breed = breed;
        return (T) this;
    }
}

public class FluffyDog extends Dog<FluffyDog> {
    private Long fluffiness;
    public <T extends FluffyDog> T setFluffiness(Long fluffiness) {
        this.fluffiness = fluffiness;
        return (T) this;
    }
}

如果您想允许FluffyDog被覆盖并提供相同的模式,那么您将不得不再次使用类中的泛型类型做同样的事情Dog

于 2018-10-30T17:50:08.877 回答