电の岁月---点滴记忆
交流,分享,进步

参考http://www.cnblogs.com/QinBaoBei/archive/2010/05/22/1741661.html

在 ICloneable 中只公开了唯一的一个方法,也就是 Clone(),您可以通过实现这个接口来完成 . Net 中原型模式的使用。

例子:通过调节RGB的值来显示颜色的变化

 

namespace WindowsFormsApplication5
{
public class ConColor:ICloneable
{
private int r, g, b;
public int R
{
get { return this.r; }
set { this.r = value; }
}
public int G
{
get { return this.g; }
set { this.g = value; }
}
public int B
{
get { return this.b; }
set { this.b = value; }
}
public ConColor(int r, int g, int b)
{
this.r = r;
this.g = g;
this.b = b;
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
}

 

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
ConColor inicolor
= new ConColor(0,0,0);//初始化一个ConColor对象
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ConColor newcolor
= (ConColor)inicolor.Clone();//浅复制初始化的对象
draw(newcolor);
}

public void draw(ConColor c)
{
Color color
=Color.FromArgb(c.R,c.G,c.B);
panel1.BackColor
= color;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
inicolor.R
= trackBar1.Value;
}

private void trackBar2_Scroll(object sender, EventArgs e)
{
inicolor.G
= trackBar2.Value;
}

private void trackBar3_Scroll(object sender, EventArgs e)
{
inicolor.B
= trackBar3.Value;
}
}
}
posted on 2010-08-12 09:52  doucontorl  阅读(169)  评论(0编辑  收藏  举报