C#:接口继承getter / setter方法 [英] C#: interface inheritance getters/setters

查看:324
本文介绍了C#:接口继承getter / setter方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组接口,这是在特定的可变对象紧密结合使用。

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 object, and then only a few properties. To avoid namespace pollution (easier intellisense) and to get across the usage intent, I'd like to have a small base interface which only exposes a few "key" properties in a read-only fashion.

然而,几乎所有的实现将支持全接口,其中包括可修改

However, almost all implementations will support the full interface, which includes modifiability.

不幸的是,我遇到了表达在C#这个概念的障碍:

Unfortunately, I ran into a roadblock expressing that concept in C#:

interface IBasicProps {
   public int Priority { get; }
   public string Name {get;}
   //... whatever
}

interface IBasicPropsWriteable:IBasicProps  {
   public int Priority { set; } //warning CS0108: [...] hides inherited member [...]
   public string Name { set; }
   //... whatever
}



我当然不有意隐藏任何成员,使AINT好!

I certainly wasn't intending to hide any members, so that aint good!

当然,我可以解决这个使用方法就好了,但有什么的右键的选择?我想保持核心界面尽可能小,即使拆分接口提供不超过沟通的意图其他用途。随着分裂的接口,它只是真的很明显这些方法都不会做任何更新,它使编写代码更清楚一点(何况还可以让漂亮的正简单的静态单存根足够了不少简单的情况下)

Of course, I can solve this using methods just fine, but what's the right choice? I'd like to keep the "core" interface as small as possible even if splitting the interfaces serves no purpose other than communicating intent. With split interfaces, it's just really obvious which methods aren't going to do any updating, and it makes writing code a bit clearer (not to mention also allows nice-n-simple static singleton stubs that suffice for quite a few simple cases).

我想,以避免任何抽象类等等;他们做出重新实施或快速单一用途的垫片一切更加复杂和难以神交。

I'd like to avoid any abstract classes and the like; they make reimplementation or quick single-purpose shims all that more complex and hard-to-grok.

所以,想法?

推荐答案

在接口方法隐藏是几乎没有蹩脚的;我会去的东西,如:

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

这篇关于C#:接口继承getter / setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆