什么是 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";
}
}
作者头像
作者名称
作者性别



评论列表