Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在不订阅的情况下触发事件_C#_Events_Interface - Fatal编程技术网

C# 在不订阅的情况下触发事件

C# 在不订阅的情况下触发事件,c#,events,interface,C#,Events,Interface,我目前正在编写一个简单游戏库,主要是供我自己使用。 现在我遇到了一个问题。我有一个叫做“游戏”的类,它看起来像这样: class Game { private List<Entity> entities; private int counter; public Game() { entities = new List<Entity>(); counter = 0; } public Ad

我目前正在编写一个简单游戏库,主要是供我自己使用。 现在我遇到了一个问题。我有一个叫做“游戏”的类,它看起来像这样:

class Game
{

    private List<Entity> entities;
    private int counter;

    public Game()
    {
        entities = new List<Entity>();
        counter = 0;
    }

    public AddEntity(Entity entity_)
    {
        entities.Add(entity_);
        // problem! how to inform entity_ that it was added?

        counter++;
    }
}
类游戏
{
私人名单实体;
专用int计数器;
公共游戏()
{
实体=新列表();
计数器=0;
}
公共加法(实体u)
{
实体。添加(实体);
//问题!如何通知实体它已添加?
计数器++;
}
}
实体是一个类,游戏中的每个对象都必须从中派生。它的内容其实并不重要。我所寻找的是一种让游戏向新添加的实体类通知其所有者(当前游戏实例)及其id(即“计数器”的用途)的方法。现在,我一直在考虑使用一个接口,该接口将有一个方法“OnAdd(Game owner,int id)”,因为这肯定会起作用,但我想确保没有比这更好的方法。这就是我的问题:


对于我的问题,有比接口更好的解决方案吗?实体实例不知道它被添加到哪个游戏实例中,并且在我看来,使用类似事件的方法是不对的。如果当然是的话,我可能会错。

是的似乎是正确的。但我们需要增加一个区别

接口-一旦更改,您需要跟踪所有实现


抽象类-可以声明虚拟函数,因此无需跟踪实现

是似乎正确。但我们需要增加一个区别

接口-一旦更改,您需要跟踪所有实现


抽象类-可以声明虚拟函数,因此无需跟踪实现

如果您的
实体
具有
游戏
类型的属性,则即使不使用事件也可以轻松解决此问题:

... code ...
entities.Add(entity_);
entity_.AddToGame(this);
... code ...

然后在
AddToGame
方法中,您可以在事件处理程序中执行任何操作,而这现在是不必要的。

如果您的
实体
具有类型为
Game
的属性,则即使不使用事件也可以轻松解决此问题:

... code ...
entities.Add(entity_);
entity_.AddToGame(this);
... code ...

然后在
AddToGame
方法中,您可以在事件处理程序中执行任何操作,而现在这是不必要的。

有很多解决方案

当然,您可以在
之后调用方法。添加

entity_.HasBeenAdded();
另一个解决办法是“撤销添加”。比如:

实体类:

public Entity
{

    ...

    public void AddTo(IList<Entity> list)
    {
        if (list == null)
        {
            throw new ArgumentNullException();
        }

        list.Add(this);

        // Do some logic here, as you now know your object
        // has been added.
    }
}

...

entity_.AddTo(entities);
公共实体
{
...
公共无效添加到(IList列表)
{
if(list==null)
{
抛出新ArgumentNullException();
}
列表。添加(此);
//在这里做一些逻辑,因为你现在知道了你的目标
//已添加。
}
}
...
实体添加到(实体);

有很多解决方案

当然,您可以在
之后调用方法。添加

entity_.HasBeenAdded();
另一个解决办法是“撤销添加”。比如:

实体类:

public Entity
{

    ...

    public void AddTo(IList<Entity> list)
    {
        if (list == null)
        {
            throw new ArgumentNullException();
        }

        list.Add(this);

        // Do some logic here, as you now know your object
        // has been added.
    }
}

...

entity_.AddTo(entities);
公共实体
{
...
公共无效添加到(IList列表)
{
if(list==null)
{
抛出新ArgumentNullException();
}
列表。添加(此);
//在这里做一些逻辑,因为你现在知道了你的目标
//已添加。
}
}
...
实体添加到(实体);

我不相信这是支持抽象类而不是接口的有效论点我不相信这是支持抽象类而不是接口的有效论点+1用于对象责任的“翻转”。聪明+1用于对象责任的“翻转”。