我这里有一个名为IFish
. 我想用WalkingFishCommon
提供不完整实现的抽象类 () 派生它,因此派生自的类WalkingFishCommon
不必实现该CanWalk
属性:
interface IFish
{
bool Swim();
bool CanWalk { get; }
}
abstract class WalkingFishCommon : IFish
{
bool IFish.CanWalk { get { return true; } }
// (1) Error: must declare a body, because it is not marked
// abstract, extern, or partial
// bool IFish.Swim();
// (2) Error: the modifier 'abstract' is not valid for this item
// abstract bool IFish.Swim();
// (3): If no declaration is provided, compiler says
// "WalkingFishCommon does not implement member IFish.Swim()"
// {no declaration}
// (4) Error: the modifier 'virtual' is not valid for this item
// virtual bool IFish.Swim();
// (5) Compiles, but fails to force derived class to implement Swim()
bool IFish.Swim() { return true; }
}
我还没有发现如何让编译器满意,同时仍然实现强制从 WalkingFishCommon 派生的类实现该Swim()
方法的目标。特别令人费解的是 (1) 和 (2) 之间的增量,编译器在抱怨Swim()
未标记为抽象的情况下交替出现,然后在下一次呼吸中抱怨它不能标记为抽象。有趣的错误!
有什么帮助吗?