实现创建代表一个人的Person类,此外创建两个继承Person类的学生Student类和老师Teacher类,创建各自的实例并输出。
Person类:
public class Person {
String name;
String sex;
int age;
Person(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
public void Print(){
System.out.print(name+" "+sex+" "+age+" ");
}
}
Student类:
public class Student extends Person {
String sno;
String prof;
Student(String name, String sex, int age,String sno,String prof){
super(name, sex, age);
this.sno=sno;
this.prof=prof;
}
public void Print(){
super.Print();
System.out.print(sno+" "+prof);
}
}
Teacher类:
public class Teacher extends Person{
String dep;
String fun;
Teacher(String name,String sex,int age,String dep,String fun){
super(name, sex, age);
this.dep=dep;
this.fun=fun;
}
public void Print(){
super.Print();
System.out.print(dep+" "+fun);
}
}
public class Build {
public static void main(String[] args) {
Show(new Person("王德发","男",100));
Show(new Student("王德发","男",100,"3012322223","软件"));
Show(new Teacher("罗玉凤","女",30,"教务处","科员"));
}
public static void Show(Person a){//向上转型
a.Print();
System.out.println();
}
}
王德发 男 100
王德发 男 100 3012322223 软件
罗玉凤 女 30 教务处 科员