Java继承类有没有懂的
设计一 个学生类Student,并派出小学生,中学生,大学生其中大学生再派出大一,二,三,四年级学生4个类:属性全部使用封装,方法:无参,有参方法: get, set方法,自我介绍方法
学生类属性:姓名,年龄,性别
小学生属性:学号,年级
中学生属性:学号,年级,政治面貌
大学生属性:系别,专业
大一,二,三,四属性:学号,班级,政治面貌
程序代码:
// 学生类
class Student {
private String name;
private int age;
private String gender;
public Student() {}
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public void introduce() {
System.out.println("我是一名学生,我的名字是" + name + ",今年" + age + "岁,性别是" + gender);
}
}
// 小学生类
class PrimaryStudent extends Student {
private String studentNo;
private int grade;
public PrimaryStudent() {}
public PrimaryStudent(String name, int age, String gender, String studentNo, int grade) {
super(name, age, gender);
this.studentNo = studentNo;
this.grade = grade;
}
public String getStudentNo() { return studentNo; }
public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
public int getGrade() { return grade; }
public void setGrade(int grade) { this.grade = grade; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是小学生,我的学号是" + studentNo + ",今年" + grade + "年级");
}
}
// 中学生类
class MiddleStudent extends Student {
private String studentNo;
private int grade;
private String politicalStatus;
public MiddleStudent() {}
public MiddleStudent(String name, int age, String gender, String studentNo, int grade, String politicalStatus) {
super(name, age, gender);
this.studentNo = studentNo;
this.grade = grade;
this.politicalStatus = politicalStatus;
}
public String getStudentNo() { return studentNo; }
public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
public int getGrade() { return grade; }
public void setGrade(int grade) { this.grade = grade; }
public String getPoliticalStatus() { return politicalStatus; }
public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是中学生,我的学号是" + studentNo + ",今年" + grade + "年级,政治面貌是" + politicalStatus);
}
}
// 大学生类
class CollegeStudent extends Student {
private String department;
private String major;
public CollegeStudent() {}
public CollegeStudent(String name, int age, String gender, String department, String major) {
super(name, age, gender);
this.department = department;
this.major = major;
}
public String getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
public String getMajor() { return major; }
public void setMajor(String major) { this.major = major; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是大学生,所在系别是" + department + ",专业是" + major);
}
}
// 大一学生类
class CollegeFreshman extends CollegeStudent {
private String studentNo;
private String className;
private String politicalStatus;
public CollegeFreshman() {}
public CollegeFreshman(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
super(name, age, gender, department, major);
this.studentNo = studentNo;
this.className = className;
this.politicalStatus = politicalStatus;
}
public String getStudentNo() { return studentNo; }
public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public String getPoliticalStatus() { return politicalStatus; }
public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是大一学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
}
}
// 大二学生类
class CollegeSophomore extends CollegeStudent {
private String studentNo;
private String className;
private String politicalStatus;
public CollegeSophomore() {}
public CollegeSophomore(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
super(name, age, gender, department, major);
this.studentNo = studentNo;
this.className = className;
this.politicalStatus = politicalStatus;
}
public String getStudentNo() { return studentNo; }
public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public String getPoliticalStatus() { return politicalStatus; }
public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是大二学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
}
}
// 大三学生类
class CollegeJunior extends CollegeStudent {
private String studentNo;
private String className;
private String politicalStatus;
public CollegeJunior() {}
public CollegeJunior(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
super(name, age, gender, department, major);
this.studentNo = studentNo;
this.className = className;
this.politicalStatus = politicalStatus;
}
public String getStudentNo() { return studentNo; }
public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public String getPoliticalStatus() { return politicalStatus; }
public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是大三学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
}
}
// 大四学生类
class CollegeSenior extends CollegeStudent {
private String studentNo;
private String className;
private String politicalStatus;
public CollegeSenior() {}
public CollegeSenior(String name, int age, String gender, String department, String major, String studentNo, String className, String politicalStatus) {
super(name, age, gender, department, major);
this.studentNo = studentNo;
this.className = className;
this.politicalStatus = politicalStatus;
}
public String getStudentNo() { return studentNo; }
public void setStudentNo(String studentNo) { this.studentNo = studentNo; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public String getPoliticalStatus() { return politicalStatus; }
public void setPoliticalStatus(String politicalStatus) { this.politicalStatus = politicalStatus; }
@Override
public void introduce() {
super.introduce();
System.out.println("我是大四学生,我的学号是" + studentNo + ",所在班级是" + className + ",政治面貌是" + politicalStatus);
}
}
