抽象类的使用,咖啡
package jsu.stu.oil;
public class CoffeStore {
private CoffeFactory factory;
public Coffee ordercoffee (String type){
Coffee coffee =factory.creatcoffee(type);
return coffee;
}
}
package jsu.stu.oil;
public class CoffeFactory {
private Coffee Coffee =null;
private Coffee Redcoffee =null;
public Blackcoffee creatcoffee(String type) {
if ("Black".equals(type)) {
return new Blackcoffee();
} else if ("Red".equals((type))) {
return new Redcoffee();
}
return null;
}
}
package jsu.stu.oil;
public class Redcoffee extends Coffee {
@Override
public String getName() {
return "Redcoffee";
}
}
package jsu.stu.oil;
public abstract class Coffee {
public abstract String getName();
public void addmilk(){
System.out.println("add milk");
}
}
package jsu.stu.oil;
public class Blackcoffee extends Coffee{
@Override
public String getName(){
return "Blackcoffee";
}
}
package jsu.stu.oil;
public class Client {
public static void main(String[] args) {
CoffeStore store = new CoffeStore();
Coffee coffee = store.ordercoffee("black");
System.out.println(coffee.getName());
System.out.println(coffee);
}
}