接口的方法实现 [英] Method implementation of interfaces

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

问题描述

如果一个类正在实现两个接口,并且这两个接口都有一个具有相同名称的方法。那有可能吗?如果是,那么该类将如何实现这两种方法?

解决方案

我想你需要看一下这个链接中的问题和答案,



了解C#中的界面 [ ^ ]

假设两个接口都规定了相同的方法签名。重载不会帮助你,因为名称和参数没有区别。您可以使用完全限定的接口成员名称。以下是一些很好的示例: http://stackoverflow.com/questions/7080861/c-sharp- interface-method-ambiguity [ ^ ]

如果你想以不同的方式实现所有方式,或者其中之一,这取决于你 - 这将满足所有的接口。但在第一种情况下,您需要在调用时限定方法。


这绝对是可能的。您需要使用显式接口实现:

  interface  IFirst { void  SomeMethod(); } 
interface ISecond { void SomeMethod(); }

class 实现:IFirst,ISecond {
void IFirst .SomeMethod(){ / * 一个实现* / }
void ISecond.SomeMethod(){ / * 另一种实现* / }
} // 课程实施





通常,在很多情况下,我建议优先使用隐式实现(基于同名的公共方法)。要解决的方法名称冲突的情况证明了其中一个好处。



实际上,这种情况并不那么重要。但是还有另一种情况,你可以有明确的语法,没有接口就无法表达 - 两种不同的索引属性。如何实现下面的语法?

 MyClass myInstance =  //   ...  
myInstance [ 3 ] = 第一个案例;
myInstance [ key] = 第二种情况;





如何?以下是解决方案:

  interface  IAuxIndexer { string   [ int  index] { get ;  set ; } 
class MyClass {
string IAuxIndexer。 this [ int index] { get { / * ... * / } set { / * ... * / }}
public string [ string index] {获取 { / * ... * / } set { / * ... * / }}
}





这样,要在一个类中拥有三个不同的索引属性,你需要两个界面等。



-SA


If a class is implementing two interfaces and both the interfaces is having a method with the same name. Then Is it possible? If yes, then how that class will implement both of those methods?

解决方案

I think you need to have a look at the question and my answer in this link,

Understanding interface in C#[^]


Let''s say, both interfaces prescribe the same method signature. Overloading won''t help you, since there is no difference in name and parameters. Than you can use the fully qualified interface member names. Here are some good samples: http://stackoverflow.com/questions/7080861/c-sharp-interface-method-ambiguity[^]
It is up to you if you want to implement all in different ways, or one of them - and that will satisfy all interfacs. But in the first case, you will need to qualify the method on call.


This is absolutely possible. You need to use explicit interface implementation:

interface IFirst { void SomeMethod(); }
interface ISecond { void SomeMethod(); }

class Implementation : IFirst, ISecond {
    void IFirst.SomeMethod() { /* one implementation */ }
    void ISecond.SomeMethod() { /* another implementation */ }
} //class Implementation



Generally, in many cases I recommend to prefer explicit implementation over implicit one (based on a public method of the same name). The case of method name clash to be resolved demonstrates just one of those benefits.

Practically, this case is not so important. But there is another case where you could have clearly unambiguous syntax which is impossible to express without interfaces — two different indexed property. How to achieve the syntax below?

MyClass myInstance = //...
myInstance[3] = "first case";
myInstance["key"] = "second case";



How? Here is the solution:

interface IAuxIndexer { string this[int index] { get; set; } }
class MyClass {
    string IAuxIndexer.this[int index] { get { /*...*/ } set { /*...*/ } }
    public string this[string index] { get { /*...*/ } set { /*...*/ } }
}



This way, to have three different indexed property in one class, you need two interfaces, etc.

—SA


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

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