在c#中执行此操作的最佳OOP模式是什么?

dex*_*ter 1 c# design-patterns

代码片段无法编译,因为它只是为了展示我想要实现的内容:假设我有一个接口:

      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;}

      }
Run Code Online (Sandbox Code Playgroud)

我所有具体的AbstractWalker实例都是IWalker的类型.实现我在代码段中指定的设计的最佳方法是什么?

Mar*_*ath 10

如果你想要私有集,只需在界面中指定一个get:

  public interface IWalker 
  {
      double DistanceTravelled {get; }
  }
Run Code Online (Sandbox Code Playgroud)

然后,IWalker的实现者可以指定私有集:

  public class Walker : IWalker 
  {
      public double DistanceTravelled { get; private set;}
  }
Run Code Online (Sandbox Code Playgroud)