java简单工厂模式

时间:2021-04-08 19:02:12

 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){

if("apple".equals(calssName)){

return new Apple

}else{return new Orange}}

public class TestDemo{

public static void main(String args[]){

Fruit f=Factory.getInstance("apple");

f.eat();

}}
}