代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 简单工厂模式 * @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(); } }