public class Base<S>
{
public static Derived<S, T> Create<T>()
{
return new Derived<S, T>(); //if not public, I wont get it here.
}
}
public class Derived<S, T> : Base<S>
{
public Derived() //the problem, dont want it public
{
}
}
这是我得到的基本结构。只向基类公开构造函数,可能吗?
要求:
1)我不想要的Derived<,>
一个实例构造调用Derived<,>
类可言,通过构造函数或静态方法吧。我希望它只能通过Base<>
创建。因此,这是出:
public class Derived<S, T> : Base<S>
{
Derived()
{
}
public static Derived<S, T> Create()
{
return new Derived<S, T>();
}
}
2)Derived<,>
类本身必须是公共的(这意味着我不能私窝Derived<,>
内Base<>
)。只有这样我才能从Base<>
的静态Create
方法中返回Derived<,>
。
可能吗?
这感觉就像一个天生的设计缺陷,因为包括循环依赖(A的B继承,而B的需求了解A)。为什么工厂方法不能进入派生类? –
你能否提供一些关于*为什么*你有这些要求的背景?派生类本身是否需要可见? –
@ T.Kiley我同意这是非标准的,但实质上不是'Encoding.UTF8'等等的行为? (虽然可能不是他们如何在内部实现) –