Week 03 作业
发布于: 2020 年 06 月 24 日
作业一:手写单例模式
作业二:使用组合模式编写程序,打印输出图1的窗口,窗口组件的树结构图2,打印输出示例图3
定义接口
public interface InterfaceComponent { void print();}
定义组件抽象类
public abstract class Component implements InterfaceComponent { private String name; private List<Component> componentList; public Component(Sting name) { this.name = name; componentList = new ArrayList<>(); } public void add(Component comp) { componentList.add(comp); } public void remove(Component comp) { componentList.remove(comp); } @override public void print() { System.out.println("print " + name); for (Component comp : componentList) { comp.print(); } }}
具体组件
public class WindowForm extends Component { public WindowForm(String name) { super(name); }}public class Picture extends Component { public Picture(String name) { super(name); }}public class Button extends Component { public Button(String name) { super(name); }}public class CheckBox extends Component { public CheckBox(String name) { super(name); }}public class Frame extends Component { public Frame(String name) { super(name); }}public class Label extends Component { public Label(String name) { super(name); }}public class LinkLabel extends Component { public LinkLabel(String name) { super(name); }}public class PasswordBox extends Component { public PasswordBox(String name) { super(name); }}public class TextBox extends Component { public TextBox(String name) { super(name); }}
入口函数
public static void main(String[] args) { Component windowFormComp = new WindowForm("WINDOW窗口"); windowFormComp.add(new Picture("LOGO图片")); windowFormComp.add(new Button("登录")); windowFormComp.add(new Button("注册")); Component frameComp = new Frame("FRAME1"); frameComp.add(new Label("用户名")); frameComp.add(new TextBox("文本框")); frameComp.add(new Label("密码")); frameComp.add(new PasswordBox("密码框")); frameComp.add(new CheckBox("复选框")); frameComp.add(new TextBox("记住用户名")); frameComp.add(new LinkLabel("忘记密码")); windowFormComp.add(frameComp); windowFormComp.print();}
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 23
鱼_XueTr
关注
还未添加个人签名 2019.04.19 加入
还未添加个人简介
评论