源程序:
abstract class Person{
private String name ;
private int age ;
public Person(String name , int age){
this.setName(name) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public String getName(){
return this.name ;
}
public void setAge(int age){
this.age = age ;
}
public int getAge(){
return this.age;
}
public void say(){
System.out.println(this.getContent()) ;
}
public abstract String getContent() ;
}
class Student extends Person{
private float score ;
public Student(String name , int age , float score){
super(name , age) ;
this.setScore(score) ;
}
public void setScore(float score){
this.score = score ;
}
public float getScore(){
return this.score ;
}
public String getContent(){
return "学生信息 ————> 姓名:" + super.getName() + ";年龄:" + super.getAge() + ";成绩:" + this.getScore() ;
}
}
class Worker extends Person{
private float salary ;
public Worker(String name , int age , float salary){
super(name , age) ;
this.setSalary(salary) ;
}
public void setSalary(float salary){
this.salary = salary ;
}
public float getSalary(){
return this.salary ;
}
public String getContent(){
return "工人信息 ————> 姓名:" + super.getName() + ";年龄:" + super.getAge() + ";工资:" + this.getSalary() ;
}
}
public class Test{
public static void main(String[] args){
Person per1 = null ;
Person per2 = null ;
per1 = new Student("张三" , 20 , 99.0f) ;
per2 = new Worker("李四" , 30 , 3000.0f) ;
per1.say() ;
per2.say() ;
}
}
运行结果: