请使用代码描述:
18岁的演员景甜会吃饭(吃小龙虾)和跳舞
30岁的歌手薛之谦会吃饭(吃大闸蟹)和唱歌.
要求: 把演员和歌手的共性抽取人类中,使用抽象类和抽象方法
[Java] 纯文本查看 复制代码 public abstract class Person {
private String name;
private int age;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
//get/set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//抽象方法吃饭
public abstract void eat();
}
public class Actor extends Person{
public Actor() {
// TODO Auto-generated constructor stub
}
public Actor(String name,int age) {
this.setName(name);
this.setAge(age);
}
public void eat() {
System.out.println(this.getAge()+"岁的"+this.getName()+"在吃小龙虾");
}
public void dance() {
System.out.println(this.getAge()+"岁的"+this.getName()+"在跳白天鹅");
}
}
public class Singer extends Person{
public Singer() {
// TODO Auto-generated constructor stub
}
public Singer(String name,int age) {
this.setName(name);
this.setAge(age);
}
public void eat() {
System.out.println(this.getAge()+"岁的"+this.getName()+"在吃大闸蟹");
}
public void sing() {
System.out.println(this.getAge()+"岁的"+this.getName()+"在演唱丑八怪");
}
}
/*请使用代码描述:
18岁的演员景甜会吃饭(吃小龙虾)和跳舞
30岁的歌手薛之谦会吃饭(吃大闸蟹)和唱歌.
要求: 把演员和歌手的共性抽取人类中,使用抽象类和抽象方法*/
public class Test {
public static void main(String[] args) {
Actor a = new Actor("景甜", 18);
a.eat();
a.dance();
Singer s = new Singer("薛之谦", 30);
s.eat();
s.sing();
}
}
|