项目根目录创建一个properties文件:
Apple = cn.gry.vo.Apple
Orange = cn.gry.vo.Orange
创建接口:
interface Fruit{ public void eat() ; }
创建两个vo类:
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 getFruit(String className) throws Exception{ className = ResourceBundle.getBundle("Message",new Locale("zh","CN")).getString(initCap(className)) ; } public String initCap(String str){ return str.subString(0,1).toUpperCase() + str.subString(1,str.length()) ; } }
测试一下:
public class Test{ public static void main(String[] args){ Fruit f = Factory.getFruit("apple") ; f.eat() ; } }
输出结果:
吃苹果