写点什么

第三周课后练习 - 作业 1

用户头像
致星海
关注
发布于: 2020 年 10 月 04 日

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


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




public class Window {
protected String name; public Window(String name) { this.name = name; } public void print() { System.out.printf("print %s(%s)\n", this.getClass().getName(), name); }}
复制代码


import java.util.ArrayList;
public class Frame extends Window {
protected ArrayList<Window> printList;
public Frame(String name) { super(name); } public void add(Window w) { if (printList == null) { printList = new ArrayList<Window>(); } printList.add(w); } public void print() { super.print(); printList(); } protected void printList() { for (int i = 0; i < printList.size(); i++) { printList.get(i).print(); } }}
复制代码


public class Button extends Window {	public Button(String name) {		super(name);	}}
public class CheckBox extends Window { public CheckBox(String name) { super(name); }}
public class Lable extends Window { public Lable(String name) { super(name); }}
public class LinkLable extends Window { public LinkLable(String name) { super(name); }}
public class PasswordBox extends Window {
public PasswordBox(String name) { super(name); }}
public class Picture extends Window { public Picture(String name) { super(name); }}
public class TextBox extends Window { public TextBox(String name) { super(name); }}
public class WinForm extends Frame { public WinForm(String name) { super(name); }}
public class WindowsPrinter {
public static void main(String args[]) { Lable lableUser = new Lable("用户名"); TextBox tb = new TextBox("文本框"); Lable lablePwd = new Lable("密码"); PasswordBox pwdBox = new PasswordBox("密码框"); CheckBox checkBox = new CheckBox("复选框"); TextBox textBox = new TextBox("记住用户名"); LinkLable linkLable = new LinkLable("忘记密码"); Frame f = new Frame("FRAME1"); f.add(lableUser); f.add(tb); f.add(lablePwd); f.add(pwdBox); f.add(checkBox); f.add(textBox); f.add(linkLable); Button bLogin = new Button("登录"); Button bReg = new Button("注册"); Picture pLog = new Picture("LOGO图片"); WinForm winFrom = new WinForm("WINDOW窗口"); winFrom.add(pLog); winFrom.add(bLogin); winFrom.add(bReg); winFrom.add(f); winFrom.print(); } }

复制代码


用户头像

致星海

关注

还未添加个人签名 2017.11.30 加入

还未添加个人简介

评论

发布
暂无评论
第三周课后练习 - 作业 1