以下是一个简单的Java程序,用于输入和显示学生和教师的数据。程序中定义了一个基类Person,以及两个派生类StudentTeacher。每个类都包含了输入和显示数据的方法。

importjava.util.Scanner;//基类PersonclassPerson{protectedintno;//编号protectedStringname;//姓名publicPerson(intno,Stringname){this.no=no;this.name=name;}publicvoidinput(){System.out.print("请输入编号:");this.no=Integer.parseInt(System.in.nextLine());System.out.print("请输入姓名:");this.name=System.in.nextLine();}publicvoiddisplay(){System.out.println("编号为:"+no);System.out.println("姓名为:"+name);}}//学生类StudentclassStudentextendsPerson{privateintclassId;//班号privatedoublescore;//成绩publicStudent(intno,Stringname,intclassId,doublescore){super(no,name);this.classId=classId;this.score=score;}publicvoidinputStudentData(){super.input();System.out.print("请输入班号:");this.classId=Integer.parseInt(System.in.nextLine());System.out.print("请输入成绩:");this.score=Double.parseDouble(System.in.nextLine());}@Overridepublicvoiddisplay(){super.display();System.out.println("班号为:"+classId);System.out.println("成绩为:"+score);}}//教师类TeacherclassTeacherextendsPerson{privateStringtitle;//职称privateStringdepartment;//部门publicTeacher(intno,Stringname,Stringtitle,Stringdepartment){super(no,name);this.title=title;this.department=department;}publicvoidinputTeacherData(){super.input();System.out.print("请输入职称:");this.title=System.in.nextLine();System.out.print("请输入部门:");this.department=System.in.nextLine();}@Overridepublicvoiddisplay(){super.display();System.out.println("职称为:"+title);System.out.println("部门为:"+department);}}//主类publicclassMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);//输入学生数据System.out.println("请输入学生数量:");intstudentCount=scanner.nextInt();Student[]students=newStudent[studentCount];for(inti=0;i<studentCount;i++){System.out.println("请输入第"+(i+1)+"个学生的信息:");students[i]=newStudent(0,"",0,0.0);students[i].inputStudentData();students[i].display();}//输入教师数据System.out.println("请输入教师数量:");intteacherCount=scanner.nextInt();Teacher[]teachers=newTeacher[teacherCount];for(inti=0;i<teacherCount;i++){System.out.println("请输入第"+(i+1)+"个教师的信息:");teachers[i]=newTeacher(0,"","","");teachers[i].inputTeacherData();teachers[i].display();}scanner.close();}}

说明:

  1. Person类:包含编号和姓名,提供输入和显示方法。

  2. Student类:继承自Person类,包含班号和成绩,重写了输入和显示方法。

  3. Teacher类:继承自Person类,包含职称和部门,重写了输入和显示方法。

  4. Main类:包含主函数

点赞(0) 打赏

微信小程序

微信扫一扫体验

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部