week3 课题作业

发布于: 5 小时前

1.以下为手写单例模式代码,所用语言为python

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

以下代码实现语言为Python语言,版本为python3

#!/bin/usr/python
# -*- coding:utf-8 -*-
# 抽象一个组织类
class Component(object):
def __init__(self, name):
self.name = name
def display(self):
pass
# 叶子节点
class Picture(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
class Button(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
class Label(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
class Checkbox(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
class TextBox(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
class LinkLabe(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
class PasswordBox(Component):
def __init__(self, name):
self.name = name
def display(self):
print self.name
# 枝节点
class WindowForm(Component):
def __init__(self, name):
self.name = name
self.children = []
def add(self, comp):
self.children.append(comp)
def display(self):
print self.name
for comp in self.children:
comp.display()
class IFrame(Component):
def __init__(self, name):
self.name = name
self.children = []
def add(self, comp):
self.children.append(comp)
def display(self):
print self.name
for comp in self.children:
comp.display()
if __name__ == "__main__":
# 生成根节点
root = WindowForm(u"Print WinForm(WINDOWN 窗口)")
# 根上长出3个叶子
root.add(Picture(u'Print Picture(LOGO 图片)'))
root.add(Button(u'Print Button(登录)'))
root.add(Button(u'Print Button(注册)'))
# 根上长出树枝frame
frame_comp = IFrame(u"Print Frame(FRAME1)")
# frame上长出七个叶子
frame_comp.add(Label(u'Print Lable(用户名)'))
frame_comp.add(Label(u'Print TextBox(文本框)'))
frame_comp.add(Label(u'Print Lable(密码)'))
frame_comp.add(Label(u'Print PasswordBox(密码框)'))
frame_comp.add(Label(u'Print CheckBox(复选框)'))
frame_comp.add(Label(u'Print TextBox(记住用户名)'))
frame_comp.add(Label(u'Print LinkLable(忘记密码)'))
root.add(frame_comp)
# 显示结构
root.display()

发布于: 5 小时前 阅读数: 8
用户头像

关注

还未添加个人签名 2019.04.12 加入

还未添加个人简介

评论

发布
暂无评论
week3课题作业