【问题标题】:derived class problem using interface使用接口的派生类问题
【发布时间】:2011-11-12 01:05:44
【问题描述】:

我有接口

interface Dot
{    
    // protected Random r = new Random();
    void createdot(Rectangle clientrectangle, Control.ControlCollection Controls);
}

我使用这个接口作为我的派生类的基类

public class BlueDot : Dot 
{
    public  List<Label> bluedot = new List<Label>();
    Random r = new Random();

    public  void  createdot(Rectangle ClientRectangle, Control.ControlCollection Controls)
    {
        for (int i = 0; i < 5; i++)
        {
            var temp = new Label();

            temp.Location = new Point(r.Next(ClientRectangle.Right - 10), r.Next(ClientRectangle.Bottom - 20));
            temp.Text = "?";
            temp.Width = 10;
            temp.Height = 10;
            temp.ForeColor = System.Drawing.Color.Blue;
            temp.BackColor = System.Drawing.Color.White;
            Controls.Add(temp);
            temp.Visible = true;
            temp.Show();
            bluedot.Add(temp);
        }
    }
}

public class RedDot:Dot
{
    public List<Label> reddot = new List<Label>();
    Random r = new Random();

    public   void  createdot(Rectangle Clientrectangle,Control.ControlCollection Controls)
    {
        for (int z = 0; z < 10; z++)
        {
            var temp2 = new Label();

            temp2.Location = new Point(r.Next(Clientrectangle.Right - 10), r.Next(Clientrectangle.Bottom - 20));
            temp2.Text = "?";
            temp2.Width = 10;
            temp2.Height = 10;
            temp2.ForeColor = System.Drawing.Color.Red;
            temp2.BackColor = System.Drawing.Color.White;
            Controls.Add(temp2);
            temp2.Show();
            reddot.Add(temp2);
        }
    }

他们在这里被调用

BlueDot bdot = new BlueDot();
        RedDot rdot = new RedDot();
private void Form1_Load(object sender, EventArgs e)
{
    this.Activate();
    bdot.createdot(this.ClientRectangle,this.Controls);
    rdot.createdot(this.ClientRectangle, this.Controls);       
}

为什么即使循环执行 10 次迭代,我仍然只得到 5 个红点?

这是样本输出https://www.facebook.com/photo.php?fbid=2372861522202&set=a.1600508493859.85328.1270463960&type=1,我只是想不通其他5个红点发生了什么,应该是10个红点....

【问题讨论】:

  • 请正确格式化您的问题;见stackoverflow.com/editing-help
  • 通常在接口名称前加上“I”表示它们是一个接口,因此例如您通常会使用名称IDot而不是上面的Dot
  • bdot 和 rdot 是如何初始化的?
  • 您的红点是否被绘制在其他点之上?
  • 不是设计,是我担心的输出,请帮帮我...

标签: c# winforms visual-studio windows-7


【解决方案1】:

我不太确定,但你为什么不让你的 temp2 可见????

temp2.​​Visible=true

如果这不起作用,您能否提供两个输出的屏幕截图。有时由于窗口的大小,一个点可能位于另一个点之上。我的意思是它们实际上是重叠的。查看您的输出可能有助于解决您的问题

【讨论】:

  • 如何发布图片/截图? ??
  • 这不是问题,它的输出,我无法弄清楚输出有什么问题
  • 现在得到照片,先生。看看说明
  • 我不知道其他人,但我看不到那张照片(通过 facebook 链接)。还有其他地方(公共)可以发布吗?
  • 我尝试从我的工作电脑和手机中加载 Facebook 链接中的图片 - 都说“此内容当前不可用”
【解决方案2】:

这里不存在继承问题。问题在于随机数生成器。

在你的代码中试试这一行:

temp2.Location = new Point(10 * z, 10 * z);

更换

temp2.Location = new Point(r.Next(Clientrectangle.Right - 10), r.Next(Clientrectangle.Bottom - 20));

您会看到 5 个蓝色“?”标签和 10 个红色“?”标签

要解决弱随机数生成器的问题,请尝试播种您的随机数生成器。示例:

Random r = new Random((int)DateTime.Now.Ticks);

【讨论】:

  • 我现在看到了我的 10 个红点...但是...它位于对角线,我需要将其分散...
  • @jeo,重点是要证明继承没有问题。放回你原来的那一行,然后用我在回答中给你的那一行替换创建随机数生成器的那一行。
  • 这是一个很大的帮助.. 但我还有一个关于吃豆人吃这些标签的问题......我可以再次编辑这个问题吗?
  • 很高兴我能提供帮助。我只想问另一个问题并提到(并链接)这个作为参考/背景/上下文。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 2010-09-10
相关资源
最近更新 更多