1700457550
System.out.println(“人种生成错误!”);
1700457551
1700457552
}
1700457553
1700457554
return(T)human;
1700457555
1700457556
}
1700457557
1700457558
}
1700457559
1700457560
HumanFactory类仅有两个地方发生变化:去掉继承抽象类,并在createHuman前增加static关键字;工厂类发生变化,也同时引起了调用者NvWa的变化,如代码清单8-14示。
1700457561
1700457562
代码清单8-14 简单工厂模式中的场景类
1700457563
1700457564
public class NvWa{
1700457565
1700457566
public static void main(String[]args){
1700457567
1700457568
//女娲第一次造人,火候不足,于是白色人种产生了
1700457569
1700457570
System.out.println(”—造出的第一批人是白色人种—”);
1700457571
1700457572
Human whiteHuman=HumanFactory.createHuman(WhiteHuman.class);
1700457573
1700457574
whiteHuman.getColor();
1700457575
1700457576
whiteHuman.talk();
1700457577
1700457578
//女娲第二次造人,火候过足,于是黑色人种产生了
1700457579
1700457580
System.out.println(”\n—造出的第二批人是黑色人种—”);
1700457581
1700457582
Human blackHuman=HumanFactory.createHuman(BlackHuman.class);
1700457583
1700457584
blackHuman.getColor();
1700457585
1700457586
blackHuman.talk();
1700457587
1700457588
//第三次造人,火候刚刚好,于是黄色人种产生了
1700457589
1700457590
System.out.println(”\n—造出的第三批人是黄色人种—”);
1700457591
1700457592
Human yellowHuman=HumanFactory.createHuman(YellowHuman.class);
1700457593
1700457594
yellowHuman.getColor();
1700457595
1700457596
yellowHuman.talk();
1700457597
1700457598
}
1700457599
[
上一页 ]
[ :1.70045755e+09 ]
[
下一页 ]