[Java] 纯文本查看 复制代码
public abstract class Pet {
private String name;
public Pet() {
// TODO Auto-generated constructor stub
}
public Pet(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
//get/set
public void setName(String name) {
this.name = name;
}
public abstract void eat();
}
public class Test {
public static void main(String[] args) {
//宠物1叫tom吃鱼
new Pet("tom"){
public void eat() {
System.out.println(this.getName()+"吃鱼");
}
}.eat();
//宠物2叫小黑,啃骨头
new Pet("小黑") {
public void eat() {
System.out.println(this.getName()+"啃骨头");
}
}.eat();
}
}