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 getInstance(String classname){
Fruit f = null;
if("apple".equals(classname)){
f = new Apple();
}
if("orange".equals(classname)){
f = new Orange();
}
return f;
}
}
public class Test {
public static void main(String[] args) {
Fruit f = null;
f = Factory.getInstance("apple");
f.eat();
}
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 getInstance(String classname){
Fruit f = null;
if("apple".equals(classname)){
f = new Apple();
}
if("orange".equals(classname)){
f = new Orange();
}
return f;
}
}
public class Test {
public static void main(String[] args) {
Fruit f = null;
f = Factory.getInstance("apple");
f.eat();
}
}
运行结果:
吃苹果