接口继承和新的关键字 [英] Interface inheritance and the new keyword

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

问题描述

我想:

public interface IBase
{
    MyObject Property1 { get; set; }
}

public interface IBaseSub<T> : IBase
{
    new T Property1 { get; set; }
}

public class MyClass : IBaseSub<YourObject>
{
    public YourObject Property1 { get; set; }
}



但是,这并不编译。它给出了错误:

But this doesn't compile. It gives the error:

//This class must implement the interface member IBase.Property1

任何人都可以阐明这一些轻?我认为它应该工作。

Can anyone shed some light on this? I thought it should work..

感谢

推荐答案

IBaseSub< T> 需要 IBase的。我说需要,因为它更准确地反映比实际意义说,它继承 IBase的,这意味着覆盖,而且根本就没有与接口发生其他事情。一类,它实现 IBaseSub< T> 实际上可以说是实现这两个,像这样:

IBaseSub<T> requires IBase. I say "requires" because it more accurately reflects the practical implications than to say it "inherits" IBase, which implies overriding and other things that simply don't happen with interfaces. A class which implements IBaseSub<T> can actually be said to implement both, like so:

public class MyClass : IBase, IBaseSub<YourObject>



让我们回到我刚才说的继承 - 有没有这样的事情有接口,这意味着仅仅因为两个接口具有相同名称的属性,派生一个不重写或隐藏的基地之一。这意味着你的类必须从现在的字面上实现具有两个名称相同的属性,以满足这两个合同。你可以用明确实施做到这一点:

Going back to what I said about inheritance - there is no such thing with interfaces, which means just because both interfaces have a property with the same name, the derived one isn't overriding or hiding the base one. It means that your class must now literally implement two properties with the same name to fulfill both contracts. You can do this with explicit implementation:

public class MyClass : IBase, IBaseSub<YourObject>
{
    public YourObject Property1 { get; set; }
    MyObject IBase.Property1 { get; set; }
}

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

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