不要添加属性到接口防止创建私有/保护"设置"在派生类型? [英] Do adding properties to an interface prevent creating private/protected "set" in derived types?

查看:109
本文介绍了不要添加属性到接口防止创建私有/保护"设置"在派生类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:原来我错过了一些明显的,但我要离开,以防有人开放的问题使得其他同样明显的错误。感谢那些谁指了出来请参阅说明底部。

是否有可能有一个非公开组被重写的接口属性属性?

Is it possible to have a non-public set on a property that is overriding an interface property?

也许我有一个愚蠢的时刻,但在我看来,其在接口中定义implicily的属性要求,没有派生

Perhaps I'm having a stupid moment, but it seems to me that having a property defined in an interface implicily requires that no deriving class may provide any non-public set for said property.

Example:

例$ C>接口IField
{
BOOL的IsValid {搞定; }
}

interface IField { bool IsValid { get; } }



...而且由于界面特性可能没有访问修饰符是指:

... and since interface properties may not have accessibility modifiers means that:

class Field : IField
{
    public override bool IsValid { get; protected set; }
}



...尽管它符合接口的事实将不可能要求概念

IMO这有可能通过非公开性质进行了封装很大的负面影响,并防止一些共同使用patters为Propertes。

IMO this has large negative implications for encapsulation that might be done via non-public Properties, and prevents a number of common use patters for Propertes.

我知道,你可以创建修改一个共同的后备存储的非公开SetIsValid成员,但是这将创建一个不一致的格局,并添加什么本来是代码的噪音,如果不是必要的。

I'm aware that you can create a non-public SetIsValid member that modifies a common backing store, but that would create an inconsistent pattern and add what would otherwise be code noise were it not necessary.

有我错过了什么?

编辑:是的,我的有无的错过了什么

Yes I have missed something

我M此刻修改别人的代码,只是意识到,我被实现的接口,并从基类派生摔跤类。而我是新的C#这是什么原因造成的覆盖混乱。

I'm modifying someone else's code at the moment and just realize that the class I was wrestling with implemented the interface and derived from a base class. And I'm new to c# That's what caused the override confusion.

实际的类看起来像:

class Field : IField, BaseField
{
    public override bool IsValid { get; protected set; }
}



...其中则baseField实现的接口为好,但没有实现设定。

...where BaseField implemented the interface as well, but did not implement the set.

推荐答案

这是完全合法的。你不需要override关键字(其实它不会编译),但并没有什么东西做这个阻止你:

This is perfectly legal. You don't need the override keyword (in fact it wouldn't compile) but there's nothing stopping you from doing this:

interface IField
{
    bool IsValid { get; }
}

class Field : IField
{
    public bool IsValid { get; protected set; }
}

这篇关于不要添加属性到接口防止创建私有/保护"设置"在派生类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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