1 回答
TA贡献1815条经验 获得超12个赞
public class Test {
public static void main(String[] args) {
Teacher teacher = new Teacher("Wang Laoshi", false, 19910123, "qinghua", 3);
System.out.println(teacher.toString());
CollegeStudent collStu = new CollegeStudent("College Student", true, 19851231, "Beijingdaxue","001", 2, "Computer", 3);
System.out.println(collStu.toString());
MiddleSchStudent middStu = new MiddleSchStudent("Middle schoole student", false, 19910705, "Di yi zhongxue", "002", 17, "English", 5);
System.out.println(middStu.toString());
}
}
abstract class Person {
protected String name;
protected boolean sex;
protected int birthday;
public Person(){}
public Person(String name, boolean sex, int birthday){
this.name =name;
this.sex = sex;
this.birthday = birthday;
}
public String toString(){
return "name:" + name + ", sex:" + sex + ", birthday: " + birthday;
}
}
class Teacher extends Person{
private String school;
private int schoolType;
public Teacher(){
}
public Teacher(String name, boolean sex, int birthday, String school, int schoolType){
super(name, sex, birthday);
this.school = school;
this.schoolType = schoolType;
}
public String toString(){
return super.toString() + ", school: " + school + ", school type: " + schoolType;
}
}
abstract class Student extends Person{
protected String school;
protected String no;
protected int grade;
public Student(){
}
public Student(String name, boolean sex, int birthday, String school, String no, int grade){
super(name, sex, birthday);
this.school = school;
this.no = no;
this.grade = grade;
}
public String toString(){
return super.toString() + ", school:" + school + ", student no: " + no + ", studeng grade: " + grade;
}
}
class CollegeStudent extends Student{
private String major;
private int grade;
public CollegeStudent(){
}
public CollegeStudent(String name, boolean sex, int birthday, String school, String no, int grade, String major, int colledgeGrade){
super(name, sex, birthday,school, no,grade);
this.major = major;
this.grade = colledgeGrade;
}
public String toString(){
return super.toString() + ", major:" + major + ", grade: " + grade;
}
}
class MiddleSchStudent extends Student{
private int grade;
public MiddleSchStudent(){
}
public MiddleSchStudent(String name, boolean sex, int birthday, String school, String no, int grade, String major, int colledgeGrade){
super(name, sex, birthday,school, no,grade);
this.grade = colledgeGrade;
}
public String toString(){
return super.toString() + ", grade: " + grade;
}
}
---------------测试
name:Wang Laoshi, sex:false, birthday: 19910123, school: qinghua, school type: 3
name:College Student, sex:true, birthday: 19851231, school:Beijingdaxue, student no: 001, studeng grade: 2, major:Computer, grade: 3
name:Middle schoole student, sex:false, birthday: 19910705, school:Di yi zhongxue, student no: 002, studeng grade: 17, grade: 5
添加回答
举报