简单工厂模式

代码:

/**
 * 简单工厂模式
 * @author se
 *
 */
public class HumanFactory {
    @SuppressWarnings("unchecked")
    public static <T> T createHuman(Class<?> c){
        Human human = null;
        try {
            human = (Human) Class.forName(c.getName()).newInstance();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return (T) human;
    }
    
    public static void main(String[] args) {
        Human m=HumanFactory.createHuman(YellowPeple.class);
        m.talk();
    }
}
View Code
原文地址:https://www.cnblogs.com/Non-Tecnology/p/6052795.html