C#创建类,有麻烦.. [英] c# creating class, having troubles..

查看:83
本文介绍了C#创建类,有麻烦..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



即时通讯在首次创建课程时遇到了问题.该程序是关于狗赛跑的,所以如果您需要更多说明,请告诉我.

这是我的课程:




im havin problems creating a class for first time. The programm is about a dog race, so if you need more explanaitions tell me please.

Here is my class:


class Dog
{
   public int RaceLenght =20;
   public int StartPosition = 0;
   public PictureBox IMG;
   public int Position;
   public Random Rnd;

   public bool Run(bool status)
   {
      return status;
   }

   public void reset()
   {
      Position = StartPosition;
   }
}




这是我的表格:




Here is my form:

string[]Arr = new string[4];
Dog Dog1 = new Dog();
Dog Dog2 = new Dog();
Dog Dog3 = new Dog();
Dog Dog4 = new Dog();
int distanz1;
int distanz2;
int distanz3;
int distanz4;



运行按钮:




Run Button:


Dog1.Run(true)
Dog1.IMG = picturebox1;
Point p = picturebox1.Location;
distanz1 = Dog1.Rnd.Next(1,4);
p.X += distanz1;
Img.Location = p;



现在我将对dog2,3和4的每个对象(按钮中的内容)执行此操作.

我认为我的代码不是最好的..



now i would do this for every object, what stands in the button, for dog2,3 and 4.

i think my code isnt the very best..

推荐答案

您还没有在Dog类中创建Random类的实例:
You haven''t created an instance of the Random class within your Dog class:
class Dog
{
public int RaceLenght =20;
public int StartPosition = 0;
public PictureBox IMG;
public int Position;
public Random Rnd;

您可以添加new位:

You could add the new bit:

public Random Rnd = new Random();

这有点麻烦,因为所有狗都有以相同的随机数序列结尾的极好机会.相反,我将创建一个static随机类,并将其用于所有实例:

But that is a bit of a nuicence, since there is a very good chance that all Dogs will end up with the same sequence of random numbers. Instead, I would create a static random class and use that instead for all instances:

public static Random Rnd = new Random();
 ...
int i = Dog.Rnd.Next(1, 4);



顺便说一句:我强烈建议您的字段应该是属性-直接公开字段是一种不好的做法,因为这意味着您不考虑外部环境的影响就无法轻松更改类的内部. >


不好意思,很抱歉,浪费您的时间,我现在要解释一下.它很简单,我只需要一个编程程序,告诉终点上第一个狗是什么狗.每条狗都有另一种速度.我想用类来编写它.但是现在的问题是所有狗狗都在运行,但不是同时运行.

方法如下:

..



BTW: I would strongly suggest that your fields should be properties - it is considered bad practice to expose fields directly, as it means you can''t easily change the internals of your class without considering the effects on the outside world.



"ok im sorry wasting your time, i will now explaining it. its simple, i just want a programm that tells what dog was first on finish line. Every dog has another speed. I want to write it with classes. But now its the problem that all doggs are running, but not at same time.

Here is the method:

..

public Point p = new Point();

public bool run (bool status, int y) //y = y pixels position of images
{
while (status)
{
Position = Rnd.Next(1,4);
p.X +=Position;
p.Y = Y;
IMG.Location = p;

if(IMG.Location.X >= RaceLenght)
{
status = false;
}
Thread.Sleep(10);


}
return status;
}

"



好的.这将很难解释,所以请多多包涵.
首先,我们必须离开您的任务,转而使用更常规的Windows类型的东西.
Windows的运行方式与您之前编写的正常"程序不同,Windows正常运行的程序在行与行之间传递,并且总是以相同的方式发生.
在Windows中,除非发生事件导致其做出响应,否则什么也不会发生.这可能是用户按下按钮,键盘按钮或某些其他需要完成某些操作的应用程序.
发生这种情况时,您的代码会告诉它是什么事件,并执行事件处理程序来处理它.如果是按按钮,则可能要关闭应用程序或打开文件.但是在为您调用适当的事件处理程序之前,您不会这样做.

这听起来与您的小狗比赛无关! :laugh:
但事实并非如此.一点也不.您的代码有一个运行"方法,该方法会循环执行,直到狗到达终点线"为止-这意味着在您仍然执行运行方法的同时,其他任何事情都不会发生(这过于简单了,但它会做几个星期).因此,当您对一只狗叫奔跑"时,它会一直循环直到狗结束,然后再与下一条狗继续.

那该怎么办?
首先,我希望您忘记您曾经看过Thread.Sleep方法.好?走了吗很好-您现在不想要它,现在不需要它,而且很有可能您永远也不需要使用它,除非您的应用程序设计得很糟糕!那就算了!好吗?

您想要做的是将一只狗的图片随机向前移动,然后再移动其他狗,看看其中是否有一个赢了".这意味着您需要在人类"时间内而不是在计算机上完成这项工作-比赛可能需要几秒钟,甚至一分钟.
为此,您需要一个定期发生的事件-我们将其称为Timer Tick事件.

因此,在表单类中添加一个私有字段:

"



Ok. This is going to be a bit difficult to explain, so please bear with me.
First off, we have to move away from your task, into more general Windows type things.
Windows operates differently from "normal" programs such as you might have written before, where flow passes from line to line, and always happens the same way.
In windows nothing happens at all unless an Event occurs to cause it to do somthing in response. That could be the user pressing a button, or a keyboard button, or some other application wanting something done for it.
When that happens, your code it told what event it is, and an event handler is executed to deal with it. If it is a button press, then you might want to close your application, or open a file. But you don''t do either until the appropriate event handler is called for you.

That probably sounds irrelevant to your little dog race! :laugh:
But it isn''t. Not at all. Your code has a "run" method, which loops round until the dog reaches the "finish line" - which means that nothing else happens while you are still executing the run method (that''s an over simplification, but it''ll do for a few weeks). So when you call "run" on one dog, it just loops round until the dog finishes, and then continues with the next dog.

So, what to do?
Firstly, I want you to forget you ever saw the Thread.Sleep method. OK? Gone? Good - you don''t want it now, you don''t need it now, and it is very, very likely that you will never, ever need to use it unless your application is very badly designed! So forget it! OK?

What you want to do is move a picture of a dog a random amount forward then move the others, and see if any of them have "won". Which means you need to do it in "human" time, rather than computer time - the race should probably take a couple of seconds, maybe a minute.
To do that, you want an Event which will happen at regular intervals - we call that a Timer Tick event.

So, add a private field to your form class:

private Timer raceTimer = new Timer();


现在,在您的Form Load事件中,添加以下代码:


Now, in your Form Load event, add the following code:

raceTimer.Interval = 100;
raceTimer.Tick += new EventHandler(raceTimer_Tick);
raceTimer.Start();

将构建一个计时器,该计时器将每十分之一秒调用一次事件(100表示​​在千分之一秒内).
现在创建事件处理程序:

That builds a Timer which will call an event every tenth of a second (the 100 is in thousands of a second).
Now create the event handler:

void raceTimer_Tick(object sender, EventArgs e)
    {
    ...
    }

每十分之一秒,该方法就会被调用-因此更改您的Dog.Run方法并使其进行一次迭代,然后向前随机执行一个步骤".
然后在滴答"处理程序中,为每只狗调用运行",然后检查是否已完成.如果有,您就有赢家,可以通过调用raceTime.Stop()
停止比赛.

听起来复杂吗?并不是真的-试试看,您会明白我的意思.

Every tenth of a second, that method will be called - so change your Dog.Run method and make it do one iteration, one random "step" forward.
Then in the Tick handler call Run for each Dog, then check if any have finished. If they have, you have a winner and can stop the race by calling raceTime.Stop()


Sounds complex? It isn''t really - try it, and you will see what I mean.


这篇关于C#创建类,有麻烦..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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