定义一个抽象的食物方法
package com.zx;
//定义一个食物的抽象方法
public abstract class Food {
public abstract void taste();
}
定义食物面条
package com.zx;
public class Flour extends Food{
public void taste() {
System.out.println(“我可以做面条”);
}
}
定义食物肉类
package com.zx;
public class Meat extends Food {
public void taste() {
System.out.println(“我的很香,但是有些油腻”);
}
}
定义一个人去实现
package com.zx;
//定义以一个person类
public class Person {
public void eat(Food foot){
foot.taste();
}
}
定义一个测试类
package com.zx;
public class Test01 {
public static void main(String[] args) {
Person per = new Person();
Food meat = new Meat();
Food flour = new Flour();
per.eat(meat);
per.eat(flour);
}
}