防止重写 C# 中的各个方法

发布于 2024-10-21 18:40:46 字数 56 浏览 1 评论 0原文

我知道我可以使用sealed来防止其他类继承某个类, 但是是否可以允许继承但防止重写某些虚拟方法?

I know that I can use the sealed in order to prevent other classes to inherit a certain class,
but is it possible to allow inheritance but prevent overriding of some virtual methods?

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

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

发布评论

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

评论(6

自此以后,行同陌路 2024-10-28 18:40:46

只能重写虚拟方法。
只要省略virtual,你的方法就不会被重写。

Only virtual methods can be overridden.
Just leave out virtual, and your method will not be overridable.

不如归去 2024-10-28 18:40:46

您还可以使用 seal 修饰符来防止派生类进一步重写该方法。

看看这个:密封方法

you can also use sealed modifier to prevent a derived class from further overriding the method.

check this out: Sealed methods

苄①跕圉湢 2024-10-28 18:40:46

是的。 sealed 关键字也可以用在方法上,表示在较高继承级别上是虚拟或抽象的方法不能被进一步继承。

如果该方法一开始就不是虚拟或抽象的,不用担心;它不能被覆盖。

请注意,sealed 仅影响方法重写;方法隐藏不能以这种方式停止,因此子类仍然可以声明一个与密封方法同名和签名的new方法。

Yes. The sealed keyword can also be used on methods, to indicate that a method which is virtual or abstract at higher inheritance levels cannot be further inherited.

If the method was never virtual or abstract to begin with, no worries; it can't be overridden.

Note that sealed only affects method overriding; method hiding cannot be stopped in this way, so a child class can still declare a new method of the same name and signature as your sealed method.

唱一曲作罢 2024-10-28 18:40:46

您可以通过使抽象类本身从某些东西派生来获得sealed关键字来处理抽象类中的方法:

abstract class DocumentTemplateBase
{
    public abstract void WriteTitle();
    public abstract void WriteSections();
}

abstract class DocumentTemplate : DocumentTemplateBase
{
    public override sealed void WriteTitle() 
    {
        Console.WriteLine("Project document"); 
    }

    public override sealed void WriteSections() 
    {
        Console.WriteLine("Sections");
    }

    abstract public void WriteContent();
}

仍然从原始(现在是派生的)抽象类派生您的具体类:

class Document1_FromTemplate : DocumentTemplate
{
    public override void WriteTitle() //error!
    {
        Console.WriteLine("Project1 document");
    }

“无法覆盖继承的成员 'Dynamics.DocumentTemplate.WriteTitle()',因为它是密封的”

但是,没有什么可以阻止实现者new它:

class Document1_FromTemplate : DocumentTemplate
{
    public new void WriteTitle() //sorry! can't stop it!
    {
        Console.WriteLine("Project1 document");
    }

You can get the sealed keyword to work on a method in the abstract class by making that abstract class itself derived from something:

abstract class DocumentTemplateBase
{
    public abstract void WriteTitle();
    public abstract void WriteSections();
}

abstract class DocumentTemplate : DocumentTemplateBase
{
    public override sealed void WriteTitle() 
    {
        Console.WriteLine("Project document"); 
    }

    public override sealed void WriteSections() 
    {
        Console.WriteLine("Sections");
    }

    abstract public void WriteContent();
}

Still deriving your concrete class from the original (and now derived) abstract class:

class Document1_FromTemplate : DocumentTemplate
{
    public override void WriteTitle() //error!
    {
        Console.WriteLine("Project1 document");
    }

"cannot override inherited member 'Dynamics.DocumentTemplate.WriteTitle()' because it is sealed"

There is nothing, however to prevent an implementer from newing it:

class Document1_FromTemplate : DocumentTemplate
{
    public new void WriteTitle() //sorry! can't stop it!
    {
        Console.WriteLine("Project1 document");
    }
魔法少女 2024-10-28 18:40:46

您可以通过两种方式使用 sealed 关键字:

  1. 在类之前以避免继承。
  2. 在函数之前以避免重写。

为了允许继承,请勿将 sealed 关键字放在类之前,并避免覆盖,将 sealed 放在您不希望被覆盖的函数之前。

You can use the sealed keyword in two ways:

  1. Before a class to avoid inheritance.
  2. Before a function to avoid overriding.

To allow inheritance don’t put the sealed keyword before the class and to avoid overriding put sealed before the function which you don’t want to be overridden.

玩心态 2024-10-28 18:40:46

是的。您需要使用密封覆盖来实现它的方法。

Yes. you need to use sealed override for the method to achieve it.

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