什么是 C# 中的密封类?
在 C# 中使用 sealed 关键字的 sealed class 不可以继承。同样,还可以将 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";
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP