Week3-Homework
发布于: 2020 年 06 月 24 日
组合模式还有点问题,暂时还没想出应该怎么解决。
public abstract class Window { protected String name; public Window(String name) { this.name = name; } public abstract boolean isRootNode(); public abstract void print();}
public class Components extends Window { private String name; public Components(String name) { super(name); this.name = name; } @Override public boolean isRootNode() { return false; } @Override public void print() { System.out.println(name); }}
public class Frame extends Window { private String name; private List<Window> windows; public Frame(String name,List<Window> windows) { super(name); this.name = name; this.windows = windows; } @Override public boolean isRootNode() { return true; } @Override public void print() { for (Window win : windows) { if(win.isRootNode()){ System.out.println(win.name); } win.print(); } }}
测试main方法:
public static void main(String[] args) { //二级子节点 Components c1 = new Components("Picture"); Components c2 = new Components("Button注册"); Components c3 = new Components("Button登录"); //一级根节点 List<Window> list = new ArrayList<>(); list.add(c1); list.add(c2); list.add(c3); //三级子节点 Components c5 = new Components("Label用户名"); Components c6 = new Components("TextBox"); Components c7 = new Components("Label密码"); Components c8 = new Components("PasswordBox"); Components c9 = new Components("CheckBox"); Components c10 = new Components("TextBox"); Components c11 = new Components("LinkLabel"); List<Window> list1 = new ArrayList<>(); list1.add(c5); list1.add(c6); list1.add(c7); list1.add(c8); list1.add(c9); list1.add(c10); list1.add(c11); //二级根节点 Frame f = new Frame("Frame", list1); list.add(f); Frame f1 = new Frame("WinFrame",list); f1.print(); }
正文不能少于50个字正文不能少于50个字正文不能少于50个字正文不能少于50个字正文不能少于50个字正文不能少于50个字正文不能少于50个字正文不能少于50个字
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 42
好
关注
还未添加个人签名 2018.05.02 加入
还未添加个人简介
评论