我正在编写一个库,我希望拥有一个接口。
但是C#不允许多重继承。因此,除了其他问题之外,用户必须记住每次想要实现Skeleton时都要继承自Skeleton。
我可以使用扩展方法。
但是我需要有 <\p>。
public interface ISkeleton
{
IEnumerable<IBone> Bones { get; }
void Attach(IBone bone);
void Detach(IBone bone);
}
对于每个ISkeleton,Attach()和Detach()的实现应该是相同的。因此,它可以基本上是这样的:
public abstract class Skeleton
{
public IEnumerable<IBone> Bones { get { return _mBones; } }
public List<IBone> _mBones = new List<IBone>();
public void Attach(IBone bone)
{
bone.Transformation.ToLocal(this);
_mBones.add();
}
public void Detach(IBone bone)
{
bone.Transformation.ToWorld(this);
_mBones.Remove(bone);
}
}
但是C#不允许多重继承。因此,除了其他问题之外,用户必须记住每次想要实现Skeleton时都要继承自Skeleton。
我可以使用扩展方法。
public static class Skeleton
{
public static void Attach(this ISkeleton skeleton, IBone bone)
{
bone.Transformation.ToLocal(skeleton);
skeleton.Bones.add(bone);
}
public static void Detach(this ISkeleton skeleton, IBone bone)
{
bone.Transformation.ToWorld(this);
skeleton.Bones.Remove(bone);
}
}
但是我需要有 <\p>。
public interface ISkeleton
{
ICollection<IBone> Bones { get; }
}
我不希望这样做,因为它不是协变的,用户可以绕过Attach()和Detach()方法。
问题:我真的必须使用抽象骨架类吗?还有其他技巧或方法吗?
IEnumerable
? - Daniel A. WhiteSkeleton
/SkeletonBase
实现ISkeleton
,或者干脆完全摒弃ISkeleton
? - Oded