C#: interface inheritance getters/setters

后端 未结 4 703
一生所求
一生所求 2020-12-29 04:20

I have a set of interfaces which are used in close conjunction with particular mutable object.

Many users of the object only need the ability to read values from the

相关标签:
4条回答
  • 2020-12-29 04:33

    You could leave the interfaces unrelated and simply have your class implement both interfaces. After all the interfaces are simply defining the contract and the contracts don't need to be related. It seems like it just an optimization for you when coding to have the writeable one derive from the other, so you only have to specify one interface.

    public interface IBasicProps
    {
       int Priority { get; }
       string Name {get;}
       //... whatever
    }
    
    public interface IBasicPropsWriteable
    {
       int Priority { get; set; }
       string Name { get; set; }
       //... whatever
    }
    
    public class Foo : IBasicProps, IBasicPropsWriteable
    {
       public int Priority { get; set; }
       public string Name { get; set; }
    
       // whatever
    }
    

    If you really needed the optimization, you could create another interface that derives from both and have your classes implement that.

    public interface IBasicPropsAll : IBasicProps, IBasicPropsWriteable  { }
    
    public class Foo : IBasicPropsAll
    {
       public int Priority { get; set; }
       public string Name { get; set; }
    
       // whatever
    }
    
    0 讨论(0)
  • 2020-12-29 04:37

    If your goal is to make it clearer when reading vs. writing is allowed, then I would use separate getter and setter methods rather than properties.

    interface IBasicProps {
       int GetPriority();
       string GetName();
       //... whatever
    }
    
    interface IBasicPropsWriteable:IBasicProps  {
       void SetPriority(int priority);
       void SetName(string name);
       //... whatever
    }
    
    0 讨论(0)
  • 2020-12-29 04:55

    Method hiding in an interface isn't nearly as grungy; I'd go with something like:

    interface IBasicProps {
       int Priority { get; }
       string Name {get;}
       //... whatever
    }
    
    interface IBasicPropsWriteable:IBasicProps  {
       new int Priority { get; set; }
       new string Name { get; set; }
       //... whatever
    }
    class Foo : IBasicPropsWriteable {
        public int Priority {get;set;}
        public string Name {get;set;}
    /* optional
        int IBasicProps.Priority {get {return Priority;}}
        string IBasicProps.Name {get {return Name;}}
    */
    }
    
    0 讨论(0)
  • 2020-12-29 04:58

    One way could be to simply skip the inheritance of the interfaces. Make one read-only interface and one write-only, and implement as necessary:

    interface IBasicPropsReadable {
       int Priority { get; }
       string Name { get; }
    }
    
    interface IBasicPropsWriteable  {
       int Priority { set; }
       string Name { set; }
    }
    
    class SomeClassReadWrite : IBasicPropsReadable, IBasicPropsWriteable {
        int Priority { get; set; }
        string Name { get; set; }
    }
    
    class SomeClassReadOnly : IBasicPropsReadable {
        int Priority { get; }
        string Name { get; }
    }
    
    0 讨论(0)
提交回复
热议问题