我正在尝试为我正在编写的程序实现良好的设计模式。我有一个这样的班级结构。
abstract class SomeBase
{
public SomeObject obj { get; protected set; }
protected SomeBase(SomeObject x)
{
obj = x;
}
//Other methods and fields...
}
public class SomeDerived : SomeBase
{
public SomeDerived() : base(new SomeObject(this))
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在我确定您知道,您不能在基本构造函数中传递 this,因为此时对象尚未初始化。无论如何,我真的希望有一个解决方法。允许SomeDerived()处理基类字段的设置对我来说不是最佳实践。我想将这个新对象向上传递。