打字猴:1.700468422e+09
1700468422
1700468423 this.componentArrayList.add(component);
1700468424
1700468425 }
1700468426
1700468427 //删除一个叶子构件或树枝构件
1700468428
1700468429 public void remove(Component component){
1700468430
1700468431 this.componentArrayList.remove(component);
1700468432
1700468433 }
1700468434
1700468435 //获得分支下的所有叶子构件和树枝构件
1700468436
1700468437 public ArrayList<Component>getChildren(){
1700468438
1700468439 return this.componentArrayList;
1700468440
1700468441 }
1700468442
1700468443 }
1700468444
1700468445 树叶节点是没有子下级对象的对象,定义参加组合的原始对象行为,其通用源代码如代码清单21-20所示。
1700468446
1700468447 代码清单21-20 树叶构件
1700468448
1700468449 public class Leaf extends Component{
1700468450
1700468451 /*
1700468452
1700468453 *可以覆写父类方法
1700468454
1700468455 *public void doSomething(){
1700468456
1700468457 *
1700468458
1700468459 *}
1700468460
1700468461 */
1700468462
1700468463 }
1700468464
1700468465 场景类负责树状结构的建立,并可以通过递归方式遍历整个树,如代码清单21-21所示。
1700468466
1700468467 代码清单21-21 场景类
1700468468
1700468469 public class Client{
1700468470
1700468471 public static void main(String[]args){
[ 上一页 ]  [ :1.700468422e+09 ]  [ 下一页 ]