C# 抽象方法继承和无意中隐藏方法

发布于 2024-12-05 10:18:16 字数 765 浏览 0 评论 0原文

我有一个关于意外隐藏抽象方法的问题。

我正在创建一个基本实体类作为接口,从中创建我正在开发的游戏中的所有其他实体。

从这个实体类,我创建了几个派生类。有诸如 MovingEntity、Trigger、Door 等之类的东西...许多这些子类也有从它们派生的子类。例如,MovingEntity 将 Projectile 和 EnemyUnit 等类作为子类。

在我的基实体类中,我有 Update() 和 Render() 等抽象方法,因为我希望每个实体都实现这些方法。

然而,一旦我到达第二层,那就是我遇到问题的地方。例如,我将使用 Trigger 类。 Trigger 派生自 Entity 基类,但 Trigger 仍然有自己的子类(如 TriggerRespawning 和 TriggerLimitedLifetime)。我不想实例化 Trigger 对象,因此我可以保持该类抽象 - 我只会从 Trigger 的子类创建对象。但是我该如何处理 Trigger 应该从 Entity 实现的抽象方法呢?

我认为我可以在 Trigger 中使用与在 Entity 中使用的代码基本相同的代码。声明相同的方法、相同的名称、相同的参数,然后将其称为抽象。然后,触发器的孩子将被迫实现实际的功能。

然而,这不起作用,因为在 Trigger 类中,我的构建错误表明我隐藏了基实体类的抽象方法。

我怎样才能传递强迫最终的孩子实现这些抽象方法的想法,而不让所有中间的父母实现它们?我需要在第一轮儿童课程中使用虚拟课程吗?

到目前为止我还没有找到很好的答案,所以我决定分解并询问。预先感谢各位。

I've got a question about accidentally hiding abstract methods.

I'm creating a basic Entity class as an interface from which to create all other entities in the game I'm working on.

From this Entity class, I have created several derived classes. There are things like MovingEntity, Trigger, Door, etc... Many of these children classes also have children derived from them. For example, MovingEntity has classes like Projectile and EnemyUnit as children.

In my base Entity class, I have methods like Update() and Render() that are abstract, because I want every entity to implement these methods.

Once I get down to the second level, however, -that's- where I hit my question/problem. I'll use the Trigger class, for example. Trigger derives from the base Entity class, but Trigger still has its own children (like TriggerRespawning and TriggerLimitedLifetime). I don't want to instantiate a Trigger object, so I can keep that class abstract - I will only create objects from Trigger's children classes. But what do I do with the abstract methods that Trigger is supposed to implement from Entity?

I thought I could just basically use the same code in Trigger as I did in Entity. Declare the same method, same name, same parameters, and just call it abstract. Then, Trigger's children would be forced to implement the actual functions.

This didn't work, however, because in the Trigger class, my build errors say that I am hiding the abstract methods from the base Entity class.

How can I pass down the idea of forcing the eventual children to implement these abstract methods without making all of the parents in-between implement them? Do I need to use virtual on the first round of children classes?

I haven't been able to find a good answer on this so far, so I decided to break down and ask. Thanks in advance, guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-12-12 10:18:16

只是根本不要重新声明方法 - 最终的具体类将必须实现树上所有尚未实现的抽象方法:

public abstract class Foo
{
    public abstract int M();
}

public abstract class Bar : Foo
{
    // Concrete methods can call M() in here
}

public class Baz : Bar
{
    public override int M() { return 0; }
}

Just don't redeclare the methods at all - the eventual concrete classes will have to implement all the abstract methods still unimplemented all the way up the tree:

public abstract class Foo
{
    public abstract int M();
}

public abstract class Bar : Foo
{
    // Concrete methods can call M() in here
}

public class Baz : Bar
{
    public override int M() { return 0; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文