Week3 作业
发布于: 2020 年 11 月 08 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
1.先定义一个接口 Print
package Composite;
public interface Print {
public void print();
}
复制代码
2.WindowForm 基类,实现 Print 接口
package Composite;
import java.util.ArrayList;
import java.util.List;
public class WindowForm implements Print {
protected List<WindowForm> windowFormList = new ArrayList<WindowForm>();
protected String windName;
public WindowForm(String windName) {
this.windName = windName;
}
public void print() {
System.out.println(windName);
for (WindowForm wind:windowFormList) {
wind.print();
}
}
public void addWind(WindowForm windowForm) {
for (WindowForm wind:windowFormList) {
if (wind == windowForm){
return;
}
}
windowFormList.add(windowForm);
}
public void delWind(WindowForm windowForm) {
for (WindowForm wind:windowFormList) {
if (wind == windowForm){
windowFormList.remove(wind);
return;
}
}
}
}
复制代码
3.Picture 类
package Composite;
public class Picture extends WindowForm {
public Picture(String windName) {
super(windName);
}
}
复制代码
4.Button
package Composite;
public class Button extends WindowForm{
public Button(String windName) {
super(windName);
}
}
复制代码
5.Frame
package Composite;
public class Frame extends WindowForm {
public Frame(String windName) {
super(windName);
}
}
复制代码
6.后面的类就省略了,直接看调用代码
import Composite.Button;
import Composite.Frame;
import Composite.Picture;
import Composite.WindowForm;
public class main {
public static void main(String[] args) {
WindowForm wind = new WindowForm("Print WinForm(WINDOWS 窗口)");
wind.addWind(new Picture("Print Picture(LOGO图片)"));
wind.addWind(new Button("Print Button(登录)"));
wind.addWind(new Button("Print Button(注册)"));
// wind.print();
Frame frame = new Frame("Print Frame(FRAME1)");
frame.addWind(new Frame("Print Lable(用户名)"));
frame.addWind(new Frame("Print TextBox(文本框)"));
frame.addWind(new Frame("Print Lable(密码)"));
frame.addWind(new Frame("Print PasswordBox(密码框)"));
frame.addWind(new Frame("Print CheckBox(复选框)"));
frame.addWind(new Frame("Print TextBox(记住用户名)"));
frame.addWind(new Frame("Print LinkLable(忘记密码)"));
wind.addWind(frame);
wind.print();
}
}
复制代码
7.结果
划线
评论
复制
发布于: 2020 年 11 月 08 日阅读数: 31
lggl
关注
还未添加个人签名 2018.08.28 加入
还未添加个人简介
评论