在 C# 中,什么是密封类?


具有 sealed 关键字的 C# 中的 sealed 类不能被继承。以同样的方法,sealed 关键字可以添加到方法中。

当你对 C# 中的方法使用 sealed 修饰符时,该方法将丧失其覆盖能力。sealed 方法应该是派生类的一部分,并且该方法必须是重写方法。

我们来看一个 C# 中 sealed 类的示例 −

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Result ob = new Result();
         string str = ob.Display();

         Console.WriteLine(str);
         Console.ReadLine();
      }
   }

   public sealed class Result {
      public string Display() {
         return "Passed";
      }
   }
}

要访问 sealed 类的成员,我们需要创建该对象。在 sealed 类中创建的方法不能被继承 −

public sealed class Result {
   public string Display() {
      return "Passed";
   }
}

更新于: 2020 年 6 月 21 日

636 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.