我正在尝试创建一个c#类,但我不希望它被继承。我怎么能做到这一点?
sealed
是您正在寻找的单词,以及reference的链接
public sealed class MyClass
{
}
然后像往常一样创建你的类,但是你将无法继承它。
但是,您仍然可以从其他类继承
public sealed class MyClass : MyBaseClass
{
}
加上PostMan's
答案,我们可以通过使achieve
为Constructor
来使Private
相同。
class NotInheritable
{
private NotInheritable()
{
//making the constructor private
}
}
class Derived : NotInheritable { }
现在班级NotInheritable
将not be inherited.