您的位置:首页 > 其它

反射结合工厂设计模式的使用

2017-03-22 14:53 501 查看
package Hg

//反射结合工厂设计模式的使用

pbulic class FactoryDEMO

public static void void main(String[] args){

Fruit Fruit = Factory.getFruit("Hg.Apple");

fruit.eat();

}

}

//接口

intterface Fruit{

void eat();

}

//实现类

class Apple implements Fruit{

@Override

public void eat(){

System.out.println("吃苹果");

}

}

class pinappale implements Fruit{

@Override

public void eat(){

System.out.println("吃菠萝")

}

}

//工厂类

class
Factory{

public static Fruit getFruit(String className){

Fruit f = null;

try{

Class<?> cls = Class.forhome(className);

f = (Fruit)cls.newinstance();

}catch(ClassNotFoundException e){

//TODO Auto-generated catch block

e.printStackTrace();

}

return;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: