什么是 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 新房祝福语简短10字
14 长辈新年祝福语大全简短
15 新年给大家简短祝福语
16 生日祝福语简短独特姑姑
17 芭比的婚礼祝福语简短
18 邀请新人祝福语简短英文