前端之家收集整理的这篇文章主要介绍了
ddd、,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.io.*;
import java.util.*;
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 name)throws Exception{
Fruit f = null;
if("apple".equals(name)){
f = new Apple();
}
else if("orange".equals(name)){
f = new Orange();
}
if(f!=null){
return f;
}
else return null;
}
}
public class FactoryDemo01{
public static void main(String args[])throws Exception{
Fruit f = Factory.getInstance("apple");
f.eat();
}
}