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

编写一个程序描述以下类层次 ,其中人为一个抽象类,其属性包括:姓名、性别、出生日期等 。

Ø教师包括任课学校、类别(大学、中学、小学)属性;

Ø学生为一抽象类,其属性包括学校、学号、年级;

Ø大学生包括专业、年级属性;中学生包括年级属性;小学生也包括年级属性。每个类设计相应的构造方法和toString()方法,子类的toString()方法也可以调用父类的toString()方法。

Ø在main方法中创建一个教师和2个学生对象,输出对象信息。





青春有我
浏览 166回答 1
1回答

萧十郎

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: 3name:College Student, sex:true, birthday: 19851231, school:Beijingdaxue, student no: 001, studeng grade: 2, major:Computer, grade: 3name:Middle schoole student, sex:false, birthday: 19910705, school:Di yi zhongxue, student no: 002, studeng grade: 17, grade: 5
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java