接口继承 [英] Interface inheritance

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

问题描述

如果我有一个接口:

interface IFoo
{
    int Offset {get;}
}

我可以有这样的:

interface IBar: IFoo
{   
    int Offset {set;}
}

所以伊巴尔的消费者将可以设置或获取?

so consumers of IBar will be able to set or get?

推荐答案

没有,你不能!

(我是要写是,但阅读安东尼的职位,并尝试了一些调整,我发现答案是NO)

(I was about to write "Yes", but after reading Anthony's post, and trying out a few tweaks, I found the answer to be NO!)

class FooBar : IFoo, IBar
{
    public int Offset{get;set;}
}

(将产生一个警告安东尼指出,这。可以通过添加新的关键字是固定的)

(Will generate a warning as Anthony points out, which can be fixed by adding the "new" keyword.)

在尝试代码:

IBar a = new FooBar();
a.Offset = 2;
int b = a.Offset;



最后一行将产生一个编译错误,因为你已隐藏伊巴尔的偏移的制定者。

The last line will generate a compile error, since you have hidden IBar's Offset setter.

编辑:修正了在类属性的accesibillity修改。 THX安东尼!

Fixed the accesibillity modifier on the property in the class. Thx Anthony!

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

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