编写一个类学生,包括姓名,年龄两个属性,在eclipse环境自动为类产生各个属性的getter和setter方法,以及toString方法,main方法,含两个参数的构造方法,
再编写大学生类,继承学生类,增加学号属性,编写有三个参数的构造方法,和toString方法,在构造方法中调用父类构造方法给属性姓名和年龄赋值,在toString方法中调用父类toString方法,自拟数据创建一个学生对象和一个大学生对象,输出两个对象。
public class Test {
public static void main(String[] args) {
Student Student_1= new Student("HXY", 20);
CollegeStudent CollegeStudent_1= new CollegeStudent("WXY",18,"20201");
System.out.println(Student_1.toString());
System.out.println(CollegeStudent_1.toString());
}
}
class Student {
public Student(String sname, int sage) {
Sage = sage;
Sname = sname;
}
public Student(){
}
public String Sname;
public int Sage;
public int getSage() {
return Sage;
}
public void setSage(int sage) {
Sage = sage;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String toString() {
return "Student [Sname=" + Sname + ", Sage=" + Sage + "]";
}
}
class CollegeStudent extends Student{
public String CollegeStudentNumber;
public CollegeStudent(String sname, int sage, String CollegeStudentNumber) {
super(sname, sage);
this.CollegeStudentNumber=CollegeStudentNumber;
}
public String toString() {
super.toString();
return "CollegeStudent [Sname=" + Sname + ", Sage=" + Sage + ", CollegeStudentNumber="+CollegeStudentNumber+"]";
}
}