第三周 - 作业 1
发布于: 2020 年 11 月 08 日
1、请在草稿纸上手写一个单例模式的实现代码
2、请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3
package mainimport ("fmt")type Component interface { Print()}type Form struct { Name string Child [] Component}func(form *Form) Add(child Component) { form.Child = append(form.Child, child)}func(form Form) Print() { fmt.Println(form.Name) for _, child := range(form.Child) { child.Print() }}type Picture struct { Name string}type Button struct { Name string}type Frame struct { Name string Child [] Component}type Label struct { Name string}type Linkable struct { Name string}type CheckBox struct { Name string}type TextBox struct { Name string}type PasswordBox struct { Name string}func(pic Picture) Print() { fmt.Println(pic.Name)}func(button Button) Print() { fmt.Println(button.Name)}func(frame Frame) Print() { fmt.Println(frame.Name) for _, child := range(frame.Child) { child.Print() }}func(frame *Frame) Add(child Component) { frame.Child = append(frame.Child, child)}func(label Label) Print() { fmt.Println(label.Name)}func(link Linkable) Print() { fmt.Println(link.Name)}func(checkbox CheckBox) Print() { fmt.Println(checkbox.Name)}func(textbox TextBox) Print() { fmt.Println(textbox.Name)}func(passbox PasswordBox) Print() { fmt.Println(passbox.Name)}func main() { FormOne := Form{Name:"WINDOW窗口"} FormOne.Add(Picture{Name: "LOGO图片"}) FormOne.Add(Button{Name: "登录"}) FormOne.Add(Button{Name: "注册"}) FrameOne := Frame{Name: "FRAME1"} FrameOne.Add(Label{Name: "用户名"}) FrameOne.Add(TextBox{Name: "文本框"}) FrameOne.Add(Label{Name: "密码"}) FrameOne.Add(PasswordBox{Name: "密码框"}) FrameOne.Add(CheckBox{Name: "复选框"}) FrameOne.Add(TextBox{Name: "记住用户名"}) FrameOne.Add(Linkable{Name: "忘记密码"}) FormOne.Add(FrameOne) FormOne.Print()}
运行结果:
划线
评论
复制
发布于: 2020 年 11 月 08 日阅读数: 39
Mr_No爱学习
关注
还未添加个人签名 2019.09.13 加入
还未添加个人简介
评论