<分区>
我在重复有关嵌套类的知识,我看到了这个:
In C#, the user is allowed to inherit a nested class from the outer class
不知道有人用过吗?使用像这样继承的类的有效方法是什么?
class A
{
class B:A
{
}
}
标签 c# class inheritance nested
<分区>
我在重复有关嵌套类的知识,我看到了这个:
In C#, the user is allowed to inherit a nested class from the outer class
不知道有人用过吗?使用像这样继承的类的有效方法是什么?
class A
{
class B:A
{
}
}
最佳答案
嵌套类可以访问其封闭类型的所有成员,包括构造函数。您可以使用私有(private)构造函数来创建具有固定数量子类的类。
public abstract class ResponseCode
{
public abstract int NumericCode { get; }
private ResponseCode() { }
public sealed class Success : ResponseCode
{
public override int NumericCode => 200;
}
public sealed class Error : ResponseCode
{
public override int NumericCode => 500;
}
}
关于c# - 使用从外部类继承嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57058539/