public class Demo_9 {
public static void main(String[] args) {
Teacher teacher = new Teacher(11111,"张三", "男", 25, "讲师");
teacher.display();
Student student = new Student(9922, "李四", "男", 21, "公寓");
student.display();
}
}
abstract class Person{
protected String name;
protected String sex;
protected int age;
protected String personInfo;
abstract String getPersonInfo();
public void display(){
javax.swing.JOptionPane.showMessageDialog(null,personInfo);
}
}
class Teacher extends Person{
protected int tnumbers;
protected String title;
public Teacher(int tnumbers, String name, String sex, int age, String title){
this.name = name;
this.sex = sex;
this.age = age;
this.tnumbers = tnumbers;
this.title = title;
personInfo=getPersonInfo();
}
public String getPersonInfo(){
return
" 职工号 " +tnumbers+ " 名字 " +name
+ " 性别 " +sex
+ " 年龄 " +age
+ " 职称 " +title;
}
}
class Student extends Person{
protected int snumbers;
protected String address;
public Student(int snumbers, String name, String sex, int age, String address){
this.name = name;
this.sex = sex;
this.age = age;
this.snumbers = snumbers;
this.address = address;
personInfo=getPersonInfo();
}
public String getPersonInfo(){
return
" 学号 " +snumbers
+ " 名字 " +name
+ " 性别 " +sex
+ " 年龄 " +age
+ " 家庭住址 " +address;
}
} |