第三周作业一

发布于: 23 小时前

手写单例代码如下:

2.组合模式打印

效果图如下:

C#代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace WindowsFormsApp2

{

public abstract class Component

{

protected string name;

public Component(string name)

{

this.name = name;

}

public abstract string Display();

}

public class Leaf : Component

{

public Leaf(string name) : base(name) { }

public override string Display()

{

return name;

}

}

public class Composite:Component

{

public Composite(string name) : base(name) { }

private List<Component> children = new List<Component>();

public void add(Component component)

{

children.Add(component);

}

public override string Display()

{

string displayString = name + "\n";

foreach (Component component in children)

{

displayString = displayString + component.Display()+"\n";

}

return displayString;

}

}

}

--------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApp2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

// 生成树根,并为其增加两个叶子节点

Composite root = new Composite(this.Name+"("+ this.Text+")");

root.add(new Leaf(pictureBox1.Name+("(Logo图片)")));

root.add(new Leaf(button1.Name+("(登录)")));

root.add(new Leaf(button2.Name + ("(注册)")));

// 为根增加两个枝节点

Composite branchX = new Composite(panel1.Name+ "(Frame1)");

root.add(branchX);

// 为BranchX增加页节点

branchX.add(new Leaf(label1.Name+ "(用户名)"));

branchX.add(new Leaf(textBox1.Name + "(文本框)"));

branchX.add(new Leaf(label2.Name + "(密码)"));

branchX.add(new Leaf(textBox2.Name + "(密码框)"));

branchX.add(new Leaf(checkBox1.Name + "(复选框)"));

branchX.add(new Leaf(label3.Name + "(记住用户名)"));

branchX.add(new Leaf(linkLabel1.Name + "(忘记密码)"));

// 显示树

string sss = root.Display();

MessageBox.Show(sss);

}

}

}

发布于: 23 小时前 阅读数: 7
用户头像

飞翔的风

关注

还未添加个人签名 2018.07.05 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业一