【问题标题】:c# Calling an method from an objectc# 从对象调用方法
【发布时间】:2016-06-19 06:39:33
【问题描述】:

所以我大致知道了:

public partial class Form1 : Form
{
    GameEngine engine;
    public Form1()
    {
        engine = new GameEngine();
    }

    public void repaint()
    {

    }
}

class GameEngine
{
    public void update()
    {

    }
}

现在我想向 update() 方法添加一些内容,使其在 Form1 类的实例内部调用 repaint() 方法,其中创建了 GameEngine 类的相应对象。

在java中我可以这样做

        engine = new GameEngine()
        {
            public void repaintCaller()
            {
                repaint();
            }
        };

并在 update() 方法中调用 repaintCaller(),但这在 c# 中不起作用,现在在 c# 中执行此操作的等效方法是什么?

【问题讨论】:

  • 我认为您的问题有点违反 C# 的面向对象原则。您可以为 GameEngine 提供对 Form 的引用,但我认为这将是糟糕的设计。在它们之间建立第三类似乎更明智

标签: c# object methods instance


【解决方案1】:

获得它的一种方法是:

public partial class Form1 : Form
{
    GameEngine engine;
    public Form1()
    {
        engine = new GameEngine();
        engine.update(this);
    }

    public void repaint()
    {

    }
}

class GameEngine
{
    public void update(Form1 form)
    {
        form.repaint();
    }
}

【讨论】:

    【解决方案2】:

    您可以向GameEngine.update 方法传递一个Action 委托给表单实例中的repaint 方法

    public partial class Form1 : Form
    {
        GameEngine engine;
        public Form1()
        {
            engine = new GameEngine();
            // I put the call here for demo purpose,
            // of course you call the engine.update 
            // method when you need and where you want 
            engine.update(repaint)
        }
    
        public void repaint()
        {
            Console.WriteLine("repaint called in the Form1 instance");
        }
    }
    
    class GameEngine
    {
        public void update(Action clientUpdate)
        {
           if(clientUpdate != null)
              clientUpdate.Invoke();
              // or just... clientUpdate();
        }
    }
    

    C# 中的parameterless Action delegate 是一种将方法(重绘)作为参数传递给被调用方法(更新)的方法。当然,您可以将整个 Form1 实例传递给更新,但这种方法将 GameEngine 类绑定到 Form1 类。使用 Action 方法,您可以摆脱这种耦合,您可以传递任何其他返回 void 并且不采用在程序的任何类中定义的任何参数的方法。这会将您的 GameEngine.update 方法从调用者的任何特定绑定中释放出来。

    【讨论】:

      【解决方案3】:

      我会尝试类似的方法

      class GameEngine
      {
          public void update(ref Form1 caller)
          {
              caller.Refresh() //msdn says "Forces the control to invalidate its client area and immediately redraw itself and any child controls."
          }
      }
      
      public partial class Form1 : Form
      {
          [...]
          engine = new GameEngine()
          engine.update(ref This)
      }
      

      我什么都不确定,我不习惯 C#。 我只是希望它会有所帮助:)

      【讨论】:

      • 有什么理由使用 ref 关键字传递表单?
      【解决方案4】:

      您还可以从表单中设置事件,如下所示:

      public partial class Form1 : Form
      {
          GameEngine engine;
      
          public Form1()
          {
              InitializeComponent();
              engine = new GameEngine();
              engine.repaintRequired += engine_repaintRequired;
              engine.update();
          }
      
          private void engine_repaintRequired(object sender, EventArgs e)
          {
              repaint();
          }
      
          public void repaint()
          {
              Console.Write("repaint");
          }
      }
      
      class GameEngine
      {
          public event EventHandler repaintRequired;
      
          public void update()
          {
              onRepaintRequired();
          }
      
          private void onRepaintRequired()
          {
              if (repaintRequired != null)
                  repaintRequired(this, new EventArgs());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多