Java 初始化子类中的父变量

Java 初始化子类中的父变量,java,Java,这是我的家长班 abstract public class Person { private String name; private Date birthday; private double difficulty; protected abstract String personType(); protected abstract Person clone(); Person(String name, Date birthday, double difficulty) { thi

这是我的家长班

abstract public class Person {
private String name;
private Date birthday;
private double difficulty;

protected abstract String personType();
protected abstract Person clone();

Person(String name, Date birthday, double difficulty) {
    this.name = name;
    this.birthday = birthday;
    this.difficulty = difficulty;
}

Person(Person copy) {
    this.name = copy.name;
    this.birthday = copy.birthday;
    this.difficulty = copy.difficulty;
}
Person(){
    this.name = "";
    this.birthday = new Date();
    this.difficulty = 0;
}

public String getName() {
    return this.name;
}

public Date getBirthday() {
    return this.birthday;
}

public double getDifficulty() {
    return this.difficulty;
}
我想用相同的构造函数变量创建一个名为Singer的子类。我的问题是,如何通过调用父类Person来初始化Singer子类中的变量“name”、“birth”和“难点”

public class Singer extends Person{
String debutAlbum;
Date debutAlbumReleaseDate;

Singer(String name, Date birthday, double difficulty, String debutAlbum, Date debutAlbumReleaseDate){
    this.debutAlbum = debutAlbum;
    this.debutAlbumReleaseDate = debutAlbumReleaseDate;
    //im not sure what to put here for name, birthday, and difficulty
}   

}

您可以使用以下三种方法中的一种:

  • 使用super()方法(推荐): 在构造函数的第一行中添加此行
    super(姓名、生日、难度)
    。 此行将调用对象的
    Person
    类的构造函数。 注意:超级方法只能在构造函数的第一行中使用
  • Add set()方法: 在Person类中,为每个变量添加set方法,然后在构造函数中调用它们
  • 更改访问修改: 将
    Person
    类中每个变量的访问修改更改为
    protected
    public
    ,然后使用
    this.name=name

  • 使用
    super(姓名、生日、难度)
    在子类构造函数中使用。请勾选并向上投票,让其他人觉得这个答案很有帮助。