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(姓名、生日、难度)
。
此行将调用对象的Person
类的构造函数。
注意:超级方法只能在构造函数的第一行中使用Person
类中每个变量的访问修改更改为protected
或public
,然后使用this.name=name代码>
使用
super(姓名、生日、难度)
在子类构造函数中使用。请勾选并向上投票,让其他人觉得这个答案很有帮助。