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){
1700468472
1700468473
//创建一个根节点
1700468474
1700468475
Composite root=new Composite();
1700468476
1700468477
root.doSomething();
1700468478
1700468479
//创建一个树枝构件
1700468480
1700468481
Composite branch=new Composite();
1700468482
1700468483
//创建一个叶子节点
1700468484
1700468485
Leaf leaf=new Leaf();
1700468486
1700468487
//建立整体
1700468488
1700468489
root.add(branch);
1700468490
1700468491
branch.add(leaf);
1700468492
1700468493
}
1700468494
1700468495
//通过递归遍历树
[
上一页 ]
[ :1.700468446e+09 ]
[
下一页 ]