从引用主类的“子”类调用函数

时间:2010-10-27 13:02:43

标签: c# design-patterns

我有一些难以解释我的问题,因为我的英语也不好:|! 我有以下代码

namespace StighyGames.MyGames {
public class MyGames
{    
...
...
  CarClass myCar = new CarClass(...); 

  Main () {
     MyGames game = MyGames(...);
  }
}

游戏是我的“主要”对象(我的应用程序)。

在我的CarClass中我有一些代码......

现在,在myCar(对象)里面我想调用一个函数(一个方法)包含主类MyGames ..方法“GameOver”。 这,因为,如果我的汽车“爆炸”我将调用GameOver方法。 但GameOver方法不能成为myCar的“孩子”...因为GameOver是游戏的一种方法...... 好的..希望得到解释,我的问题是:“我不知道如何调用主对象类的方法”..

感谢您的帮助!

5 个答案:

答案 0 :(得分:5)

您有几种选择。

1)在CarClass中创建event并在MyGames中捕获它。

2)使用delegate并将函数引用传递给CarClass对象

3)将属性添加到MyCames类型的CarClass(称为“所有者”),并在创建CarClass后,为其指定“this”:myCar.Owner=this。因此,您已在CarClass对象中为其创建者创建了一个引用,CarClass中的代码可以直接访问其所有者的方法。

哪个最好取决于具体情况以及如何使用这些对象。事件可能是首选,因为它提供了最大的灵活性。代表的灵活性和效率都低于事件,尽管它们实际上的用途略有不同。 (事件实际上是代表的扩展)。最后一个可能是最糟糕的形式,一般来说,因为它紧紧地绑定物体,但它有时间和地点。

这是#1

public class CarClass
{
    // A delegate is a pointer to a function. Events are really a way of late
    // binding delegates, so you need to one of these first to have an event.
    public delegate void ExplodedHandler(CarClass sender);
    // an event is a construct which is used to pass a delegate to another object
    // You create an event based for a delegate, and capture it by
    // assigning the function reference to it after the object is created. 
    public event ExplodedHandler Exploded;
    protected void CarCrash()
    {
       ... do local stuff
       // Make sure ref exists before calling; if its required that something
       //  outside this object always happen as a result of the crash then 
       // handle the other case too (throw an error, e.g.)/
       // See comments below for explanation of the need to copy the event ref
       // before testing against null
       ExplodedHandler tempEvent = Exploded;
       if (tempEvent != null) {
         tempEvent(this);
       } 
    }
}
public class MyGames
{    
...
...
  CarClass myCar = new CarClass(...); 
  myCar.Exploded += new ExplodedHandler(GameOver);
  Main () {
     MyGames game = MyGames(...);
  }
  void GameOver(CarClass sender) {
     // Do stuff

  }
}

答案 1 :(得分:1)

你可以:

  1. 修改您的CarClass课程(真正应该重命名为Car)以保留对其所属游戏的引用。

  2. 在名为Explode的Car类上创建一个事件。然后,Game类可以注册Explode事件,并确保从该处理程序调用{​​{1}}方法。

答案 2 :(得分:1)

您可以在主类上使用Singleton设计模式。

或者你可以恢复问题,并在MyGames中实现CheckGameOver,它会调查汽车的状态。这将是我首选的方法,因为它减少了依赖性。

答案 3 :(得分:1)

您可以将对MyGames实例的引用传递给CarClass实例,它是“父”引用。虽然我认为您可以使用事件和委托更好地构建游戏,但没有什么可以阻止您在父对象上调用方法。

答案 4 :(得分:1)

众所周知,你有几种选择。但并非所有选项都同样出色 这是我的清单。

1)向Car对象添加一个事件,让游戏注册到它 2)将代表传递给Car对象
3)将对象传递给Car对象 4)使用静态呼叫。

除了第一个以外的所有答案都是“坏”;)

这里的代码。我正在使用C#4.0

public class Car
{
    public event Action<Car> Explode;

    private void OnExplode()
    {
        Explode(this);
    }
}

public class Game
{
    private Car car;


    public Game()
    {
        car = new Car();
        car.Explode += new Action<Car>(NotifyExplosion);
    }

    private void NotifyExplosion(Car car)
    {
        Console.WriteLine("{0} is exploded", car.ToString());
        GameOver();
    }

    private void GameOver()
    {
        Console.WriteLine("GAME OVER");
    }

    static void Main(string[] args)
    {
        Game game = new Game();
    }

}