为了账号安全,请及时绑定邮箱和手机立即绑定

如图,可以给个代码的思路吗?

如图,可以给个代码的思路吗?

青春有我 2023-04-10 16:12:50
编写一个程序描述以下类层次 ,其中人为一个抽象类,其属性包括:姓名、性别、出生日期等 。Ø教师包括任课学校、类别(大学、中学、小学)属性;Ø学生为一抽象类,其属性包括学校、学号、年级;Ø大学生包括专业、年级属性;中学生包括年级属性;小学生也包括年级属性。每个类设计相应的构造方法和toString()方法,子类的toString()方法也可以调用父类的toString()方法。Ø在main方法中创建一个教师和2个学生对象,输出对象信息。
查看完整描述

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


查看完整回答
反对 回复 2023-04-12
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信