1 package Test; 2 3 public class Solution { 4 public static void main(String[] args)throws InstantiationException, IllegalAccessException, ClassNotFoundException 5 { 6 Factory factory=new Factory(); 7 IMan man=factory.createMan(); 8 IWoman woman=factory.createWoman(); 9 man.speak(); 10 woman.speak(); 11 } 12 13 } 14 interface IMan 15 { 16 public void speak(); 17 } 18 interface IWoman 19 { 20 public void speak(); 21 } 22 class WhilteMan implements IMan 23 { 24 public void speak() 25 { 26 System.out.println("我是白皮肤男人"); 27 } 28 } 29 class WhilteWoman implements IWoman 30 { 31 public void speak() 32 { 33 System.out.println("我是白皮肤女人"); 34 } 35 } 36 class YellowMan implements IMan 37 { 38 public void speak() 39 { 40 System.out.println("我是黄皮肤男人"); 41 } 42 } 43 class YellowWoman implements IWoman 44 { 45 public void speak() 46 { 47 System.out.println("我是黄皮肤女人"); 48 } 49 } 50 class Factory 51 { private static String classPath="Test.Yellow"; 52 public static IMan createMan() throws InstantiationException, IllegalAccessException, ClassNotFoundException 53 { 54 String name=classPath+"Man"; 55 return (IMan)Class.forName(name).newInstance(); 56 } 57 public static IWoman createWoman() throws InstantiationException, IllegalAccessException, ClassNotFoundException 58 { 59 String name=classPath+"Woman"; 60 return (IWoman)Class.forName(name).newInstance(); 61 } 62 }