c# - 使用从外部类继承嵌套类

标签 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/

相关文章:

Javascript继承与简洁的原型(prototype)赋值语法

python - python中的父/子类结构

c# - Entity Framework - 未设置外键(0/空)但导航属性不为空

python - Scipy.Stats.exponweib 的子类不调用 __init__ 或其他函数

c++ - C++中的继承

c++ - 基类未定义

java - 以类为键的 TreeMap

c# - 隐式转换不可分配给接口(interface)

c# - 无需用户干预即可获取访问 token C# SDK

c# - 如何更改 MSMQ 中消息的优先级?