gpt4 book ai didi

java - 有什么方法可以在 Java 中返回多态 this 吗?

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:21 26 4
gpt4 key购买 nike

在 Typescript 中,有一个多态返回类型 this 的概念。 https://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?

最佳答案

另外两个选项:

  1. 由于 setter 方法的顺序并不重要,在功能上,请按自下而上的顺序调用它们。如果您先分配给变量,则无需转换。

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

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

关于java - 有什么方法可以在 Java 中返回多态 this 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53069411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com