[英]Different objects added to the list become same
i'm trying to learn C# with graphics, using sdl.net at the moment. 我正在尝试用图形学习C#,目前使用sdl.net。 I wanted to learn how to create objects on screen, but stuck on this crazy thing for three days.
我想学习如何在屏幕上创建对象,但坚持这个疯狂的事情三天。 Googled everything i could think of, but can't solve this on my own and i'm asking somebody for help.
谷歌搜索我能想到的一切,但不能自己解决这个问题,我要求别人帮忙。
I simplified the example to bare minimum. 我简化了这个例子。 For the moment i try to create a few objects by clicking mouse on screen and print their numbers.
目前我尝试通过在屏幕上单击鼠标并打印他们的数字来创建一些对象。 On creation, every object gets a number, i put it on the list and then foreach every object.
在创建时,每个对象都得到一个数字,我将它放在列表中,然后预先处理每个对象。
But when i print the object list, every object becomes like the last one, though constructor shows that it creates new object with new number. 但是当我打印对象列表时,每个对象都变得像最后一个,尽管构造函数显示它创建了具有新数字的新对象。 But when i iterate the list, every object has same number.
但是当我迭代列表时,每个对象都有相同的数字。
Constructor: object number:1
Show() object number:1
object count:1
Constructor: object number:2
Show() object number:2
Show() object number:2
object count:2
Constructor: object number:3
Show() object number:3
Show() object number:3
Show() object number:3
object count:3
When it should be: 它应该是:
Constructor: object number:3
Show() object number:1
Show() object number:2
Show() object number:3
object count:3
What am i doing wrong, what am i missing?? 我做错了什么,我错过了什么? (i tried same principle with windows forms and buttons, and it worked fine)
(我尝试了与窗体和按钮相同的原理,它工作正常)
My simplified code: 我的简化代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using SdlDotNet.Graphics;
using SdlDotNet.Input;
using SdlDotNet.Core;
using Font = SdlDotNet.Graphics.Font;
public class KeyboardTest
{
private static Surface m_VideoScreen;
private static Surface m_DrawingSurface;
private static List<ball> ball_list=new List<ball>();//### THE OBJECT LIST ###
private static int num = 1;
class ball //### MY CLASS ###
{
private static int numeris;
public ball(int _numeris) //### CONSTRUCTOR ###
{
numeris = _numeris;
System.Diagnostics.Debug.WriteLine("Constructor: object number:{0}", numeris);
}
public void show() //### VOID TO PRINT OBJECT NUMBER ###
{
System.Diagnostics.Debug.WriteLine("Show() object number:{0}", numeris);
}
}
public static void Main(string[] args) // ### MAIN VOID, SKIP THIS ###
{
m_VideoScreen = Video.SetVideoMode(800, 600, 32, false, false, false, true, true);
m_VideoScreen.Fill(Color.White);
m_DrawingSurface = Video.CreateRgbSurface(m_VideoScreen.Width, m_VideoScreen.Height, 32, 0, 0, 0, 0, true);
m_DrawingSurface.Fill(Color.White);
Events.Quit += new EventHandler<QuitEventArgs>(ApplicationQuitEventHandler);
Events.Tick += new EventHandler<TickEventArgs>(ApplicationTickEventHandler);
Events.TargetFps = 1;
Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(ApplicationMouseButtonEventHandler);
Events.Run();
}
private static void ApplicationMouseButtonEventHandler(object sender, MouseButtonEventArgs args)
{
if (args.Button == MouseButton.PrimaryButton && args.ButtonPressed==true)
{
ball_list.Add(new ball(num)); //### ADDING TO THE LIST ON MOUSE CLICK ###
num += 1;
}
}
private static void ApplicationTickEventHandler(object sender, TickEventArgs args)
{
m_VideoScreen.Blit(m_DrawingSurface);
foreach (ball temp in ball_list) //### PRINTING NUMBERS OF ALL OBJECTS IN LIST ###
{
temp.show();
}
m_VideoScreen.Update();
System.Diagnostics.Debug.WriteLine("object count:{0}", ball_list.Count);
}
private static void ApplicationQuitEventHandler(object sender, QuitEventArgs args)
{
Events.QuitApplication();
}
}
Your issue is that you're using a static variable: 您的问题是您使用的是静态变量:
private static int numeris;
Change it to be an instance: 将其更改为实例:
private int numeris;
In your ball
class, you have: private static int numeris;
在你的
ball
,你有: private static int numeris;
A static variable is one that's at the class level, not at the instance level. 静态变量是在类级别而不是在实例级别的变量。 Your ball instances all are sharing that number.
你的球实例都在分享这个数字。
从Ball类中删除numeris
中的Static
关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.