接口继承Q? [英] Interface Inheritance Q?

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

问题描述

我创建了以下内容:

I have created the following:

<br />public interface ICalculatable<br />{<br />    DateTime FirstPaymentDate { get; }<br />    decimal MonthlyCompoundingRate { get; }<br />}<br /><br />public interface IBaseObject : ICalculatable<br />{<br />    string Identifier { get; }<br />   // Input Value<br />    DateTime FirstPaymentDate { get; set; }<br />   // Input Value<br />    decimal InterestRate { get; set; }<br />}<br /><br />public class BaseObject : IBaseObject<br />{<br />    ...<br />}<br />



我遇到了一个编译器错误,该错误需要重新生成才能隐藏继承的成员.我试图找出这是否正确,或者我需要使用virtual/override/etc...

我将在其他代码中以ICalculatable的身份访问BaseObject,并且FirstPaymentDate属性应为相同的值/来源.

先谢谢了.



I get a compiler error that need new to hide inherited member. I am trying to figure out if that is the correct thing or I need to use virtual/override/etc.. instead.

I will be accessing the BaseObject as ICalculatable in other code and the FirstPaymentDate properties should be the same value/source.

Thanks in advance.

推荐答案

在ICalculatable中,FirstPaymentDate在IBaseObject(实现ICalculatable)中仅具有get

,它具有get和set ,因此编译器说它没有使用相同的签名实现,因此您需要添加新关键字以在ICalculatable

中像这样

in ICalculatable FirstPaymentDate just has a get

in IBaseObject (which implements ICalculatable) it has a get and a set, so the compiler is saying its not being implemented with the same signature, therefore you need to add the new keyword to hide the base FirstPaymentDate in ICalculatable

like so

<br />    public interface ICalculatable<br />    {<br />        DateTime FirstPaymentDate { get; }  <br />        decimal MonthlyCompoundingRate { get; }<br />    }<br />    <br />    public interface IBaseObject : ICalculatable<br />    {<br />        string Identifier { get; }<br />        new DateTime FirstPaymentDate { get; set; }<br />        decimal InterestRate { get; set; }<br />    }


一样隐藏基本的FirstPaymentDate.
编辑
我刚刚重新阅读了您的问题,上面的内容并没有帮助您[D''Oh]

最好的方法是在演示项目中尝试一下



EDIT
I have just reread your question and the above does not help does it [D''Oh]

Best way is to try it out in a demo project


Ware @ Work写道:
Ware@Work wrote:

FirstPaymentDate属性应具有相同的值/源.

the FirstPaymentDate properties should be the same value/source.



IBaseObject没有值/源.只有实施者这样做,所以这与您的方案无关.实现者有可能明确实现两个接口,从而为实现的类提供不同的值/源.我从来没有尝试过,因为,这似乎是个坏主意.

此外,可能存在与您正在使用的两个接口不同的设计,这些设计可能会更适合您的需求.也许阅读艾伦·霍尔布(Allen Holub)讨论了为什么Getter和Setters都是邪恶的 [ ^ ] 会帮助你.



IBaseObject has no value/source. Only the implementer does so this is not relevant to your scenario. It might be possible for the implementer to explicitly implement the two interfaces giving the implemented class different value/source. I have never tried it because, well it seems like a bad idea.

Also it is possible that there is different design than the two interfaces you are using that might better suit your needs. Perhaps reading Allen Holub discuss why Getters and Setters are Evil[^] will help you.






这篇关于接口继承Q?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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