的代碼片段將無法編譯,因爲它只是爲了展示我想實現什麼: 說我有一個接口:什麼是最好的面向對象模式在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