什么是 C# 中的密封类?
C#中带有sealed关键字的Sealed类不能被继承。同样,可以在方法中添加sealed关键字。
当您在C#中对方法使用密封修饰符时,该方法将失去其覆盖功能。密封方法应该是派生类的一部分,并且该方法必须是重写的方法。
让我们看看C#中密封类的示例-
示例
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";
}
}
}要访问密封类的成员,我们需要创建对象。在密封类中创建的方法不能被继承-
public sealed class Result {
public string Display() {
return "Passed";
}
}热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短