c#接口列表 [英] c# List of interfaces

查看:93
本文介绍了c#接口列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我必须做像画图的应用程序.我必须画线,矩形和椭圆形.我想使用STATE deisgn patern.我制作了Line,Rect和Ellipse和Interface IPaint类.


在班级主修中,我制作了IPaint油漆,而List< IPaint> list = new Line< IPaint>();然后通过3个按钮,我添加paint = new Line();或rect或ellipse()取决于单击的夹心按钮.但是当我使用list.Add(paint)时这无法正常工作.

当我添加第一个元素时可以,但是当我添加第二个元素时,第一个元素看起来像第二个.当我添加thirth元素时,请先添加,然后再添加el.具有像thitrh这样的价值.

我该怎么做对呢?

/////edit
是的,这只是帖子中的错字.在代码中是



I have to do application like Paint. I have to paint Line, Rectangule and Ellipse. I want to use STATE deisgn patern. I made class Line, Rect and Ellipse and Interface IPaint.


In class main I made IPaint paint, and List<IPaint> list= new Line<IPaint>(); Then by 3 buttons i add paint = new Line(); or rect or ellipse() depends on wich button are clicked. But when I`m use list.Add(paint) this not working correct.

When i add first element it`s OK, But when i add second element then first element look like second. When i`m add thirth element then first, and second el. have value like thitrh.

How can i do this right ?

/////edit
Yes it`s just a typo in post. In code is

List<IPaint> list = new List<IPaint>;



我现在在工作,我没有访问代码的权限.
但是完整的代码如下所示:

接口IPaint



Im in work now and i dont have acces to code.
But full code look like:

Interface IPaint

Interface IPaint
{
void Paint{PrintEvenArgs e}
Point BeginPoint{get;set;}
Point EndPoint(get;set)
}



Class Line,Rect和Ellipse外观相同:



Class Line, Rect and Ellipse look the same:

Class Line: IPaint{
public Begin/Endpoint {get;set;}
void Paint(PrintEventArgs e)
{
Pen p = new Pen(Color red,2)
e.drawLine(p,BeginPoint,EndPoint)
}
//in Rect and ellipse only change drawRectangle or DrawEllipse
}



在Form1中,我这样做:



In Form1 i do:

Class Main{
IPaint point;
List<IPaint> lista = new List<IPaint>();

 Void Main
 {


    Inicianization();
 }

  event MouseDown(Obiect Sender, MouseArgs e)
  {
     point.BeginPoint = e.location;

  }
  
  event mouseMove(...)
  {
    point.EndPoint = e.location;
    Invalidate();
  }

  event mouseup(..)
  {
    lista.Add(point);
  }

event Form1_Paint(Object Sender, PrintEventArgs e)

  If (!Point.BeginPoint.IsNull & !Point.EndPoint.IsnuLL)
  {
    point.Paint(e);
  }

  for (int i =0, i< lista.Count, i++)
  {
    lista[i].Paint(e);
  }

event button1_click(...)
{
point = new Line();
// button 2 - point = New Rectangle or ellipse button 3
}
}




问题在添加到列表中.当我添加第一个元素的秒数为(2,2)m秒(3,3)时,则首先将值更改为列表中第二个元素,当我添加第三个元素的点数为(5,6)时,则第一个元素和第二个元素的更改值到第三个元素和列表如下:

清单[0]-起点-5
-端点-6
清单[1]-起点-5
-端点-6
清单[2]-起点-5
-端点-6


看来当我添加到列表对象点时,它是通过引用添加的.但是为什么呢?




Problem is in add to list. When i Add first element with point`s (2,2)m second (3,3) then first change value to second element on the list, when i add thirth with points (5,6) then first and secont element change value to thirth element and list look like:

list[0] - Begin point - 5
- EndPoint - 6
list[1] - Begin point - 5
- EndPoint - 6
list[2] - Begin point - 5
- EndPoint - 6


It is look like when i add to list object point, it is add by reference. But why ?

推荐答案

List<IPaint> list= new Line<IPaint>();



这里有一个错字;你应该写:



There is a typo here ; you should rather write :

List<IPaint> list = new List<IPaint>();



如果这只是您帖子中的错字(而不是您的实际代码中的错字),那么您是否使三个类(Line,Rectangle,Ellipse)实现了IPaint?



If this is just a typo in your post (and not in your actual code), did you make your three classes (Line, Rectangle, Ellipse) implement IPaint ?


我在列表上始终添加相同的对象point,当我更改对象的值时,我又一次将其添加到列表中,但是相同的对象及其所有内容均通过引用提供.

我必须将MouseMove事件更改为:
Like I sad before, i add on list always the same Object point, when i change value of obiect i one more time add to list but the same obiect and its everthing is by reference.

I have to change in MouseMove event to:
if Line
point = New Line();
if rect;// 
point = New Rect();
else point = (New Ellipse();

point.EndPoint = e.location


这篇关于c#接口列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆