总结;定义一个接口,多个实现类,一个工厂方法
public interface Human { void eat(); void sleep(); }
public class WhiteHuman implements Human{ @Override public void eat() { System.out.println("白种人吃饭"); } @Override public void sleep() { System.out.println("白种人睡觉"); } }
public class YellowHuman implements Human{ @Override public void eat() { System.out.println("黄种人吃饭"); } @Override public void sleep() { System.out.println("黄种人睡觉"); } }
public class HumanFactory { public static Human createHuman(Class tClass) { try { return (Human) Class.forName(tClass.getName()).newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } }
测试:
标签:java,void,System,模式,工厂,Human,Override,public,out From: https://www.cnblogs.com/liubaihui/p/16751143.html