interface Fruit{
public void eat();
};
class Orange implements Fruit{
public void eat(){
System.out.println("eat orange");
}
}
class Apple implements Fruit{
public void eat(){
System.out.println("eat apple");
}
}
class Factory{
public static Fruit getFruit(String className){
Fruit f=null;
if("apple".equals(className)){
f=new Apple();
}
if("orange".equals(className)){
f=new Orange();
}
return f;
}
}
public class Demo027{
public static void main(String[] args){
Fruit f=Factory.getFruit(args[0]);
if(f!=null){
f.eat();
}
}
}
JavaLearning:工厂设计模式
最新推荐文章于 2025-04-27 12:30:03 发布
