Client.java
上传用户:hensond
上传日期:2021-12-27
资源大小:817k
文件大小:1k
- package com.company.section5;
- /**
- * @author cbf4Life cbf4life@126.com
- * I'm glad to share my knowledge with you all.
- */
- public class Client {
- public static void main(String[] args) {
- //创建一个根节点
- Composite root = new Composite();
- root.doSomething();
-
- //创建一个树枝构件
- Composite branch = new Composite();
- //创建一个叶子节点
- Leaf leaf = new Leaf();
-
- //建立整体
- root.add(branch);
- branch.add(leaf);
- }
-
- //通过递归遍历树
- public static void display(Component root){
- for(Component c:root.getChildren()){
- if(c instanceof Leaf){ //叶子节点
- c.doSomething();
- }else{ //树枝节点
- display(c);
- }
- }
-
- }
- }