的代码片段将无法编译,因为它只是为了展示我想实现什么: 说我有一个接口:什么是最好的面向对象模式在C#中做到这一点
public Interface IWalker
{
//Compiles but not what I need
double DistanceTravelled {get; set;}
//Compiler error - cant be private for set, but that's what I need
//double DistanceTravelled {get; private set;}
}
public abstract AbstractWalker : IWalker
{
//Error:Cannot implement - but thats what I need
//protected double DistanceTravelled {get; private set}
//Error the set is not public and I dont want the property to be public
//public double DistanceTravelled { get;private set; }
//Compiles but not what i need at all since I want a protected
// property -a. and have it behave as readonly - b. but
// need it to be a part of the interface -c.
public double DistanceTravlled {get; set;}
}
我所有的混凝土AbstractWalker的实例实际上是IWalker的类型。 实现我在代码片段中指定的设计的最佳方式是什么?
是否有某些原因需要成为界面?抽象类基本上是一样的,除了你可以添加受保护的方法。 – 2010-11-25 21:39:58