抽象方法覆盖抽象方法

发布于 2024-08-12 02:38:44 字数 340 浏览 4 评论 0原文

public abstract class A
{
    public abstract void Process();
}

public abstract class B : A
{
    public abstract override void Process();
}

public class C : B
{
    public override void Process()
    {
        Console.WriteLine("abc");
    }
}

此代码引发编译错误:“B”未实现继承的抽象成员“A.Process()”。

有什么办法可以做到这一点吗?

public abstract class A
{
    public abstract void Process();
}

public abstract class B : A
{
    public abstract override void Process();
}

public class C : B
{
    public override void Process()
    {
        Console.WriteLine("abc");
    }
}

This code throws an Compilation Error: 'B' does not implement inherited abstract member 'A.Process()'.

Is there any way to do this?

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

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

发布评论

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

评论(4

笛声青案梦长安 2024-08-19 02:38:45

只需在类 B 中完全忽略该方法即可。无论如何,B 都会从 A 继承该方法,并且由于 B 本身是抽象的,因此您不需要显式地再次实现它。

Just leave out the method completely in class B. B inherits it anyway from A, and since B itself is abstract, you do not explicitly need to implement it again.

新人笑 2024-08-19 02:38:45

在 VS2008 中为我工作;没有错误,没有警告。但是,没有理由在 B 中进行“覆盖”。此代码是等效的:

public abstract class A
{
    public abstract void Process();
}

public abstract class B : A
{
}

public class C : B
{
    public override void Process()
    {
        Console.WriteLine("abc");
    }
}

Works for me in VS2008; no errors, no warnings. BUT, there's no reason to have the 'override' in B. This code is equivalent:

public abstract class A
{
    public abstract void Process();
}

public abstract class B : A
{
}

public class C : B
{
    public override void Process()
    {
        Console.WriteLine("abc");
    }
}
别低头,皇冠会掉 2024-08-19 02:38:45

我有时看到这种情况的地方是用抽象方法覆盖非抽象虚拟方法。例如:

public abstract class SomeBaseType
{
    /* Override the ToString method inherited from Object with an abstract
     * method. Non-abstract types derived from SomeBaseType will have to provide
     * their own implementation of ToString() to override Object.ToString().
     */
    public abstract override string ToString();
}

public class SomeType : SomeBaseType
{
    public override string ToString()
    {
        return "This type *must* implement an override of ToString()";
    }
}

The place where I've seen this sometimes is overriding a non-abstract virtual method with an abstract method. For example:

public abstract class SomeBaseType
{
    /* Override the ToString method inherited from Object with an abstract
     * method. Non-abstract types derived from SomeBaseType will have to provide
     * their own implementation of ToString() to override Object.ToString().
     */
    public abstract override string ToString();
}

public class SomeType : SomeBaseType
{
    public override string ToString()
    {
        return "This type *must* implement an override of ToString()";
    }
}
泪意 2024-08-19 02:38:45

阿隆——

这毫无意义。首先,这实际上编译得很好。其次,您在 A 中声明的抽象方法被继承(仍然是抽象)到 B 中。因此,您无需在类 B 中声明 Process()。

--
标记

Alon -

This makes no sense. First of all, this does actually compile fine. Secondly, the abstract method you declared in A is inherited (still abstract) into B. Therefore, you have no need to declare Process() in class B.

--
Mark

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