public String getName(){ //取得name属性内容
return name;
}
public int getAge(){ //取得age属性内容
return age;
}
public void say(){ //说话是具体功能,要定义成普通方法
System.out.println(this.getContent());
}
public abstract String getContent(); //说话内容由子类定义
}[/code]定义Student继承Person类[code=java]package cn.itcast.heima
public class Student extends Person{ //定义Student继承Person类
private float score;
public Student(String name,int age,float score){
super(name,age); //调用父类中构造方法
this.score = score;
}
public String getContent(){ //覆写父类中的抽象方法
return "Student 信息-->姓名:"+super.getName()+
",年龄:"+super.getAge()
+",score:"+this.score;
}
}[/code]定义Worker继承Person类[code=java]package cn.itcast.heima
public class Worker extends Person{ //定义Worker继承Person类
private float salary;
public Worker(String name,int age,float salary){
super(name,age); //调用父类中构造方法
this.salary = salary;
}
public String getContent(){ //覆写父类中的抽象方法
return "Worker 信息-->姓名:"+super.getName()+
",年龄:"+super.getAge()
+",salary:"+this.salary;
}
}[/code]定义测试类[code=java]package cn.itcast.heima
public class Demo{
public static void main(String args[]){
Person student = null;
Person worker = null;
student = new Student("老罗",15,30.0f);
worker = new Worker("小罗",14,20.0f);
student.say();
worker.say();