Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OOP基础:从派生类在基构造函数中创建重写对象_C#_Oop - Fatal编程技术网

C# OOP基础:从派生类在基构造函数中创建重写对象

C# OOP基础:从派生类在基构造函数中创建重写对象,c#,oop,C#,Oop,很抱歉标题不好,但我的英语不是很好。 假设以下代码(在程序模式下使用LINQPad进行测试): 调用“new BaseClass().PrintSettings();”显然会在控制台中输出“BaseClassSetting”。 让我们从out基类派生一个类: public class InheritedClass : BaseClass { public new class Settings : BaseClass.Settings { public string

很抱歉标题不好,但我的英语不是很好。
假设以下代码(在程序模式下使用LINQPad进行测试):

调用“new BaseClass().PrintSettings();”显然会在控制台中输出“BaseClassSetting”。
让我们从out基类派生一个类:

public class InheritedClass : BaseClass
{
    public new class Settings : BaseClass.Settings
    {
        public string InheritedClassSetting { get { return "InheritedClassSetting!"; } }
    }
}
调用“new InheritedClass().PrintSettings();”仍将在控制台中打印“BaseClassSetting”。
使代码使用“新”继承类的唯一方法是添加一个类似于

public InheritedClass()
{
    this.settings = new Settings();
}
通过这个额外的ctor,它将所需的“InheritedClassSetting,BaseClassSetting”打印到控制台。

现在的问题是:是否可以使基类始终使用实例对象的设置类?(不在派生类中添加ctor)

如果实践不好,则问题的解决方法可能不好。我一般不会使用
new
关键字,更不用说嵌套类了。通过使基类接受从
BaseClass.Settings
继承的泛型类型并使用无参数构造函数,可以删除继承类中的构造函数。显然,这是不可读的

public class BaseClass<T> where T : BaseClass<T>.Settings, new()
{
    protected Settings settings;

    public BaseClass()
    {
        settings = new T();
    }

    public virtual void PrintSettings()
    {
        var t = settings.GetType();
        Console.WriteLine(string.Join(", ", t.GetProperties().Select(pi => pi.Name)));
    }

    public class Settings
    {
        public string BaseClassSetting
        {
            get { return "BaseClassSetting."; }
        }
    }
}

public class InheritedClass : BaseClass<InheritedClass.Settings>
{
    public new class Settings : BaseClass<InheritedClass.Settings>.Settings
    {
        public string InheritedClassSetting
        {
            get { return "InheritedClassSetting!"; }
        }
    }
}
公共类基类,其中T:BaseClass.Settings,new()
{
保护设置;
公共基类()
{
设置=新的T();
}
公共虚拟空打印设置()
{
var t=settings.GetType();
WriteLine(string.Join(“,”,t.GetProperties().Select(pi=>pi.Name));
}
公共类设置
{
公共字符串基类设置
{
获取{return“BaseClassSetting.”;}
}
}
}
公共类继承类:基类
{
公共新类设置:BaseClass.Settings
{
公共字符串继承类设置
{
获取{return“InheritedClassSetting!”;}
}
}
}
请注意,在线:

public new class Settings : BaseClass<InheritedClass.Settings>.Settings
public新类设置:BaseClass.Settings

泛型的参数不要求您指定
InheritedClass
。我添加它是因为它已经相当混乱了,我不想让它变得更糟。

不,因为不能有“虚拟类型”


有几种方法可以尝试实现您在这里所做的事情,但所有这些方法都需要
InheritedClass
在某个时候显式引用其自己版本的
设置。

不错,但确实不是很可读;-)您解决此任务的方法是什么?@Sascha取决于您如何使用您的类。例如,如果继承不是必需的,我将只使用一个接口,用于您所称的
BaseClassSettings
InheritedSettings
(但不使其中一个从另一个派生)。此外,如果不是严格必要的话,我会避免将它们声明为嵌套类。
public new class Settings : BaseClass<InheritedClass.Settings>.Settings