public interface IFruit {
public void eat();
}
public class Apple implements IFruit{
@Override
public void eat() {
System.out.println("吃苹果");
}
}
public class Orange implements IFruit {
@Override
public void eat() {
System.out.println("吃橘子");
}
}
public class FruitFactory {
public static IFruit getFruit(String classname){
IFruit fruit = null;
try {
fruit = (IFruit) Class.forName(classname).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return fruit;
}
}
public class Test {
public static void main(String[] args) {
IFruit fruit = FruitFactory.getFruit("com.wxw.ClassFactory.Apple");
fruit.eat();
}
}