c# 在派生 类 中强制执行特定方法签名(抽象虚拟静态)
c# enforcing specific method signature in derived classes (abstract virtual static)
我有一个 abstract class BaseClass
并且想要强制所有 class Derived: BaseClass
必须覆盖一个方法 foo
从而产生一个非常具体的签名。问题如下:
- 类 继承自 BaseClass 必须自己实现
foo
(pref. only if they are not abstract) [like abstract
]
- BaseClass 必须有一个 base-implementation,派生 类 可以调用 [like
virtual
]
- [签名 of
foo
in derived 类 should be static
(类 themselves are not!) [编辑:省略]]
foo
的参数 属于 class Derived
/this.GetType()
(首选不更改 BaseClass 的签名)
将其中两个组合起来已经足够困难/不可能(?)(例外 4),但也许有一些聪明的解决方法,我会尽我所能。最后这里是一些伪代码,以更好地反映我的意图:
public abstract class BaseClass
{
bool baseBool;
public abstract virtual static bool foo(BaseClass b1, BaseClass b2)
{
//sample code
return b1.baseBool ^ b2.baseBool;
}
}
public class Derived: BaseClass
{
bool derivedBool;
public static override bool foo(Derived d1, Derived d2)
{
//sample code
return base.foo(d1, d2) && d1.derivedBool ^ d2.derivedBool;
}
}
基于 lidqy 接受的答案的解决方案
public abstract class BaseClass
{
bool baseBool;
public abstract bool foo(BaseClass b1, BaseClass b2);
}
public abstract class BaseClassWrappedCRTP<T>: BaseClass where T: BaseClassWrappedCRTP<T>
{
public override bool foo(BaseClass b1, BaseClass b2)
{
//sample code
return b1.baseBool ^ b2.baseBool && fooWrappedCRTP((T)b1, (T)b2);
}
protected abstract bool fooWrappedCRTP(T d1, T d2);
}
public class Derived: BaseClassWrappedCRTP<Derived>
{
bool derivedBool;
protected override bool fooWrappedCRTP(Derived d1, Derived d2)
{
//sample code
return d1.derivedBool ^ d2.derivedBool;
}
}
请注意,如果 Derived 是 abstract class Derived: BaseClassWrappedCRTP<Derived>
,我们将需要 abstract class DerivedWrappedCRTP<T>: Derived where T: DerivedWrappedCRTP<T>
以这种方式继续继承 foo
(鉴于实际需要访问非泛型 Derived
).
如评论中所述,继承和静态不适合。
根据你所说的,我认为这段代码可能符合你的用例...
//declare the mixed lists with List<IBase> to get rid of the type params
interface IBase {
bool foo(BaseClass b1, BaseClass b2);
}
public abstract class BaseClass<T> : IBase where T : BaseClass
{
bool baseBool;
//If you call Foo from a "mixed list" use non generic, base class parameters, else you get casting troubles enumerating and calling 'foo'
public bool foo(BaseClass b1, BaseClass b2)
{
//sample code
return b1.baseBool ^ b2.baseBool && fooOverride((T)b1, (T)b2);
}
protected abstract bool fooOverride(T b1, T b2);
}
public class Derived : BaseClass<Derived>
{
bool derivedBool;
protected override bool fooOverride(Derived d1, Derived d2)
{
//sample code
return d1.derivedBool ^ d2.derivedBool;
}
}
我有一个 abstract class BaseClass
并且想要强制所有 class Derived: BaseClass
必须覆盖一个方法 foo
从而产生一个非常具体的签名。问题如下:
- 类 继承自 BaseClass 必须自己实现
foo
(pref. only if they are not abstract) [likeabstract
] - BaseClass 必须有一个 base-implementation,派生 类 可以调用 [like
virtual
] - [签名 of
foo
in derived 类 should bestatic
(类 themselves are not!) [编辑:省略]] foo
的参数 属于class Derived
/this.GetType()
(首选不更改 BaseClass 的签名)
将其中两个组合起来已经足够困难/不可能(?)(例外 4),但也许有一些聪明的解决方法,我会尽我所能。最后这里是一些伪代码,以更好地反映我的意图:
public abstract class BaseClass
{
bool baseBool;
public abstract virtual static bool foo(BaseClass b1, BaseClass b2)
{
//sample code
return b1.baseBool ^ b2.baseBool;
}
}
public class Derived: BaseClass
{
bool derivedBool;
public static override bool foo(Derived d1, Derived d2)
{
//sample code
return base.foo(d1, d2) && d1.derivedBool ^ d2.derivedBool;
}
}
基于 lidqy 接受的答案的解决方案
public abstract class BaseClass
{
bool baseBool;
public abstract bool foo(BaseClass b1, BaseClass b2);
}
public abstract class BaseClassWrappedCRTP<T>: BaseClass where T: BaseClassWrappedCRTP<T>
{
public override bool foo(BaseClass b1, BaseClass b2)
{
//sample code
return b1.baseBool ^ b2.baseBool && fooWrappedCRTP((T)b1, (T)b2);
}
protected abstract bool fooWrappedCRTP(T d1, T d2);
}
public class Derived: BaseClassWrappedCRTP<Derived>
{
bool derivedBool;
protected override bool fooWrappedCRTP(Derived d1, Derived d2)
{
//sample code
return d1.derivedBool ^ d2.derivedBool;
}
}
请注意,如果 Derived 是 abstract class Derived: BaseClassWrappedCRTP<Derived>
,我们将需要 abstract class DerivedWrappedCRTP<T>: Derived where T: DerivedWrappedCRTP<T>
以这种方式继续继承 foo
(鉴于实际需要访问非泛型 Derived
).
如评论中所述,继承和静态不适合。
根据你所说的,我认为这段代码可能符合你的用例...
//declare the mixed lists with List<IBase> to get rid of the type params
interface IBase {
bool foo(BaseClass b1, BaseClass b2);
}
public abstract class BaseClass<T> : IBase where T : BaseClass
{
bool baseBool;
//If you call Foo from a "mixed list" use non generic, base class parameters, else you get casting troubles enumerating and calling 'foo'
public bool foo(BaseClass b1, BaseClass b2)
{
//sample code
return b1.baseBool ^ b2.baseBool && fooOverride((T)b1, (T)b2);
}
protected abstract bool fooOverride(T b1, T b2);
}
public class Derived : BaseClass<Derived>
{
bool derivedBool;
protected override bool fooOverride(Derived d1, Derived d2)
{
//sample code
return d1.derivedBool ^ d2.derivedBool;
}
}