创建一个Student类,Student类在继承People类的基础上,增加了一个属性stuID(学号);Student类通过两个有参构造方法对name、age和stuID进行初始化。
class Student extends People
{
private String stuID;
Student(String name, int age, String stuID) //有参构造方法
{
//【代码1】,对成员变量name和age初始化
//【代码2】,对成员变量stuID初始化
}
Student(String stuID) //有参构造方法,name和age取默认值
{
//【代码3】,对成员变量name 和age初始化
//【代码4】,对成员变量stuID初始化
}
public void print()
{ super. print( );
System.out.println(" StuID="+stuID);
}
}创建一个Student类,Student类在继承People类的基础上,增加了一个属性stuID(学号);Student类通过两个有参构造方法对name、age和stuID进行初始化。
class Student extends People
{
private String stuID;
Student(String name, int age, String stuID) //有参构造方法
{
//【代码1】,对成员变量name和age初始化
//【代码2】,对成员变量stuID初始化
}
Student(String stuID) //有参构造方法,name和age取默认值
{
//【代码3】,对成员变量name 和age初始化
//【代码4】,对成员变量stuID初始化
}
public void print()
{ super. print( );
System.out.println(" StuID="+stuID);
}
前提 Person 类有两个入参的构造方法
3 和 4 可以合并成 this("Huazie", 18, stuID);
不知道你这个问题是否已经解决, 如果还没有解决的话:class People {
private String name;
private Integer age;
private String address;
public People(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public void display() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("籍贯:" + address);
}
}
class Student extends People {
private String stuID;
public Student(String name, Integer age, String address, String stuID) {
super(name, age, address);
this.stuID = stuID;
}
public Student(String name, Integer age, String stuID) {
this(name, age, null, stuID);
}
public void display() {
super.display();
System.out.println("学号:" + stuID);
}
}