防止覆盖/隐藏方法c#的任何子类; 保留超类的方法签名

Tri*_*shi 2 c# oop

我的问题属于这种情况

class A
{
    public virtual void show()
    {
         Console.WriteLine("Hey! This is from A;");
    }
}
class B:A
{
    public sealed override void show()
    {
         Console.WriteLine("Hey! This is from B;");
    }
}
class C:B
{
    public new void show()
    {          
         Console.WriteLine("Hey! This is from C;");         
    }          
}
Run Code Online (Sandbox Code Playgroud)

要么

class A
 {
      public  void show()
      {
           Console.WriteLine("Hey! This is from A;");
      }
 }
 class B:A
 {
      public new void show()
      {
               Console.WriteLine("Hey! This is from B;");
      }
 }
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,C类隐藏了B类的方法Show()

问:我怎么能确定没有Subclass Override以及已经在SuperClass中定义的Hides方法

像这样的东西或者可能readonly是用于字段的关键字

 class A1
 {
      public sealed void show() // I know it will give compilation error
      {
           Console.WriteLine("Hey! This is from A1");
      }
 }
 class B1 : A1
 {
      public void show()
      {
           Console.WriteLine("You must get a compilation Error if you create method with this name and parameter");
      }
 }
Run Code Online (Sandbox Code Playgroud)

有没有这样的关键词?

编辑1:

是的我想阻止扩展程序确保它使用方法名称和参数coz的正确实现,如果其他人查看代码它应该是正确的

Ser*_*rvy 13

防止存在隐藏方法的子类的唯一方法是创建类sealed,从而防止任何子类.如果可以有任何子类,那么他们可以隐藏方法,你无能为力.