当前位置: 代码迷 >> J2SE >> 工厂设计模式实现时出现异常
  详细解决方案

工厂设计模式实现时出现异常

热度:192   发布时间:2016-04-23 20:17:34.0
工厂设计模式实现时出现错误
interface Fruit{
public void eat() ;
}
class Apple implements Fruit{
public void eat(){
System.out.println("吃苹果。。。") ;
}
};
class Orange implements Fruit{
public void eat(){
System.out.println("吃橘子。。。") ;
}
};
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 FactoryDemo{
public static void main(String args[]){
Fruit f = Factory.getFruit(args[0]) ;
if(f!=null){
f.eat() ;
}
}
}

------解决思路----------------------
运行的时候 给 args 传值了么
  相关解决方案