1、+ d i s p l a y ( )- n a m e- a g eP e r s o n+ d i s p l a y ( )- n a m e- a g e- s c h o o l N a m eS t u d e n t+ d i s p l a y ( )- n a m e- a g e- y i e l dW o r k e r+ v i e w ( )V i e w e r+ m a i n ( )T e s tPerson.java 源程序清单public class Personprivate String name;private int age;public Perso
2、n()name = “;age = 0;public Person(String name,int age)this.name=name;if(age0)age = 0;this.age=age;public void setName(String name)this.name=name;public String getName()return name;public void setAge(int age)if(age0)age=0;this.age=age;public int getAge()return age;public void display()System.out.prin
3、tln(“姓名:“ + name + “t 年龄:“ + age);Student.java 源程序清单public class Student extends Personprivate String schoolName;Student()schoolName = “;Student(String name,int age,String schoolName)super(name,age);this.schoolName = schoolName;public void setSchoolName(String schoolName)this.schoolName = schoolName
4、;public String getSchoolName()return schoolName;public void display()super.display();System.out.println(“学校名称:“ + schoolName);Worker.java 源程序清单public class Worker extends Personprivate Long yield;public Worker()yield = new Long(0);public Worker(String name,int age,Long yield)super(name,age);this.yie
5、ld = yield;public void setYield(Long yield)this.yield = yield;public Long getYield()return yield;public void display()super.display();System.out.println(“产量:“+yield.longValue();Viewer.java 源程序清单public class Viewerpublic void view(Person p)p.display();Test.java 源程序清单public class Test /含有 main()方法的主类public static void main(String args)Student s = new Student(“张三“,24,“我的学堂“); /定义 Student 类变量并实例化Worker w = new Worker(“Jone“,40,new Long(1005); /定义 Worker 类变量并实例化Viewer v = new Viewer();v.view(s); /调用 Student 对象的实例方法v.view(w); /调用 Worker 对象的实例方法