写点什么

Week03 作业

用户头像
关注
发布于: 2020 年 06 月 24 日

1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。


2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。


定义一个接口

public interface IComponent {    void print();}
复制代码

定义组件抽象类

public abstract class Component implements IComponent {
private String name; private List<Component> componentList;
public Component(String name) { this.name = name; componentList = new ArrayList<>(); }
public void add(Component component) { componentList.add(component); }
public void remove(Component component) { componentList.remove(component); }
@override public void print() { System.out.println("print " + name); for (Component component : componentList) { component.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); }}
复制代码

main 函数

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();    }
复制代码


用户头像

关注

还未添加个人签名 2018.04.17 加入

还未添加个人简介

评论

发布
暂无评论
Week03 作业