第三周作业 - 命题作业

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

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



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





Tree.java

package com.demo;
public interface Tree {
void print();
}



Node.java

package com.demo;
import java.util.ArrayList;
import java.util.List;
public class Node implements Tree{
private List<Tree> children = new ArrayList<>();
private String name;
public Node(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + name);
for (Tree node : children){
node.print();
}
}
public void addChild(Tree node){
children.add(node);
}
public List<Tree> getChildren() {
return children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



Leaf.java

package com.demo;
public class Leaf implements Tree{
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



AppMain.java

package com.demo;
public class AppMain {
public static void main(String[] args) {
Node winForm = new Node("WinForm(WINDOW窗口)");
Leaf logoPicture = new Leaf("Picture(LOGO图片)");
Leaf loginButton = new Leaf("Button(登录)");
Leaf registerButton = new Leaf("Button(注册)");
Node frame = new Node("Frame(FRAME1)");
Leaf userNameLabel = new Leaf("Label(用户名)");
Leaf userNameTextBox = new Leaf("TextBox(文本框)");
Leaf passwordLabel = new Leaf("Label(密码)");
Leaf passwordBox = new Leaf("PasswordBox(密码框)");
Leaf rememberUserNameCheckBox = new Leaf("CheckBox(复选框)");
Leaf remberUserNameLabel = new Leaf("TextBox(记住用户名)");
Leaf forgetPasswordLinkLabel = new Leaf("LinkLabel(忘记密码)");
frame.addChild(userNameLabel);
frame.addChild(userNameTextBox);
frame.addChild(passwordLabel);
frame.addChild(passwordBox);
frame.addChild(rememberUserNameCheckBox);
frame.addChild(remberUserNameLabel);
frame.addChild(forgetPasswordLinkLabel);
winForm.addChild(logoPicture);
winForm.addChild(loginButton);
winForm.addChild(registerButton);
winForm.addChild(frame);
winForm.print();
}
}



用户头像

molly

关注

还未添加个人签名 2017.12.14 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业-命题作业