(习题)编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类Person,并作为学生数据操作类Student和教师数据操作类Teacher的基类。
import java.util.Scanner;
class Person{
protected int bianHao;
protected String name;
Scanner in = new Scanner(System.in);
protected int getBianHao(){
System.out.print("请输入编号:");
bianHao = in.nextInt();
return bianHao;
}
protected String getName(){
System.out.print("请输入姓名:");
name = in.next();
return name;
}
}
class Student extends Person{
public int classNum;
public int score;
public int getClassNum(){
//使用super关键字调用父类方法
super.getBianHao(); //调用父类getBianHao()方法
super.getName(); //调用父类getName()方法
System.out.print("请输入班级:");
classNum = in.nextInt();
getScore();
Stu();
return classNum;
}
public int getScore(){
System.out.print("请输入分数:");
score = in.nextInt();
System.out.println();
return score;
}
public void Stu(){
System.out.println("输出学生编号:" + bianHao);
System.out.println("输出学生姓名:" + name);
System.out.println("输出学生班级:" + classNum);
System.out.println("输出学生分数:" + score);
System.out.println();
}
}
class Teacher extends Person{
public String zhiCheng;
public String buMen;
public String getZhiCheng(){
//使用super关键字调用父类方法
super.getBianHao(); //调用父类getBianHao()方法
super.getName(); //调用父类getName()方法
System.out.print("请输入职称:");
zhiCheng = in.next();
getBuMen();
Tea();
return zhiCheng;
}
public String getBuMen(){
System.out.print("请输入部门:");
buMen = in.next();
System.out.println();
return buMen;
}
public void Tea(){
System.out.println("输出老师编号:" + bianHao);
System.out.println("输出老师姓名:" + name);
System.out.println("输出老师职称:" + zhiCheng);
System.out.println("输出老师部门:" + buMen);
}
}
public class Test3_4{
public static void main(String[] args){
Student s = new Student();
s.getClassNum();
Teacher t = new Teacher();
t.getZhiCheng();
}
}
刚学到类和对象的内容,还不甚了解虽然代码可以基本实现问题中的功能若有不对之处,请斧正。