开发者

Sealed keyword confusion

开发者 https://www.devze.com 2023-01-23 00:18 出处:网络
using System; class A { public virtual void F() { Console.WriteLine(\"A.F\"); } public virtual void G() { Console.WriteLine(\"A.G\");
using System;

class A
{
   public virtual void F() {
      Console.WriteLine("A.F");
   }
   public virtual void G() {
      Console.WriteLine("A.G");
   }
}

class B: A
{
   sealed override public void F() {
      Console.WriteLine("B.F");
   } 
   override public void G() {
      Console.WriteLine("B.G");
   } 
}

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

In the above question I want to know that the Class c has no method with name f() I mean if I create an object of it and access the method f() will it throw error or not? If yes then I want to know that because of inheritance class c should have a method with name f() of class A ... so I can开发者_运维问答t call this?


You cannot remove methods in a base class from a derived class; that would violate the LSP, and could be trivially defeated by simply casting to the base. The sealed keyword prevents any derived class from overriding the method; it doesn't hide it.

0

精彩评论

暂无评论...
验证码 换一张
取 消