Java 中子类的构造函数链接

Constructor Chaining with subclasses in Java

只是一个问题 RE: Constructor Chaining in subclasses,我找不到好的答案,我有点困惑。

我正在制作一个基本的基于文​​本的小型 RPG 进行一些练习,我正在通过我的构造函数进行抽象 class 并将 0-4 参数的构造函数链接在一起,如下所示

abstract class Creature { 

// Fields
private String name;
private int lifeForce;
private int strength;
private int agility;

// Constructors + Chaining
public Creature() { 
    this("Unknown")
}
public Creature(String name) { 
    this(name, 100);
}
public Creature(String name, int lifeForce)  {
    this(name, lifeForce, 10);
}
public Creature(String name, int lifeForce, int strength) {
    this(name, lifeForce, strength, 10);
}

public Creature(String name, int lifeForce, int strength, int agility) {
    this.name = name;
    this.lifeForce = lifeForce;
    this.strength = strength;
    this.agility = agility;
}

我的困惑是如何最好地格式化生物的子class 的构造函数,例如这个简单的 Person class 引入了两个新字段。这样写构造函数肯定重复太多了

// Constructors + Chaining
public Person() { 
    super("Unknown");
    this.skillClass=new Mage();
    this.dialogue="...";

}

public Person(String name) { 
    super(name);
    this.skillClass=new Mage();
    this.dialogue="...";
    
} etc etc etc 

我想我可以限制构造函数来限制重复,但我主要只是想知道是否有我在这里遗漏的最佳实践。

欢迎任何和所有建议,如果有人有任何好的资源可以推荐比平常更深入

Class B extends Class A

我非常欣赏的示例。

在这种情况下,当您需要使用具有不同参数的多个构造函数时,建议使用这样的构建器模式:

abstract class Creature { 

// Fields
private String name;
private int lifeForce;
private int strength;
private int agility;

private Creature(Builder<?> builder) {
    this.name = builder.name;
    this.lifeForce = builder.lifeForce;
    // Add the other attributes here.
}

public static abstract Builder extends Builder<T extends Builder<T>> {
    private String name;
    private int lifeForce;
    private int strength;
    private int agility;

    public Builder(//here you put the attributes that you need to have in all instances) {
            // here you do the affectations.
       }
       // now you need to make the functions that set each property :
       public Builder lifeForce(int lifeForce) {
              this.lifeForce = lifeForce;
              return this;
       }
       // you do the same thing for all the other attributes.
       ...
       public Creature build() {
              return new Creature(this);
       }
 }
}

所以解释:此模式将允许您通过仅设置所需的属性来创建 class 的实例。

因为这里有子classes,构建器模式会更难理解一些,但在这种情况下它是完美的解决方案。

我们还需要为每个子class应用构建器模式,所以让我们为这个人做吧 class :

public class Person extends Creature {
       private int anotherField;
       public Person(Builder builder) {
              super(builder);
              this.anotherField = anotherField;
       }
       public static Builder extends Creature.Builder<Builder> {
              public Builder(//add the fieldHere if it is needed in all class instances) {
              // if the field is not mandatory you can omit this constructor but you need to put the function below.
       }
       public Builder anotherField(int anotherField) {
              this.anotherField = anotherField;
       }
       public Person build() {
              return new Person(this);
       }
}

现在让我告诉你这个解决方案有多么棘手:

1/ 用 2 个字段声明 person :

Person p1 = Person.Builder().name("name").anotherField(0).build();

2/ 声明另一个只有一个字段的

Person p2 = Person.Builder().agility(1000).build();

备注:在这两个示例中,我假设您的构建器的构造函数没有参数。例如,如果名称是必填字段:

Person p3 = Person.Builder("name").anotherField(0).build();

我希望你对使用构建器模式有想法。