如何实现具有返回值而非void的接口 [英] How do I implement an interface with a returned value instead of void

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

问题描述

尝试实施IList< T>如下:

Trying to implement IList<T> as follows:

public class My<T> : IList<T>
{
   public bool Add(T itemm) {...}
   ...
}



这将因为返回类型是'bool'而不是接口的'void',所以不能编译,尽管实现的Add可以在接口所需的任何地方使用。例如:


This will not compile since the return type is 'bool' instead of the interface's 'void', in spite of the fact that the implemented Add can be used wherever reqired by the interface. e.g.:

IList<int> list = new My<int>(); //Had it compiled....
list.Add(10);
...

这当然只与返回类型无关。



有没有办法让实现匹配界面?



谢谢,



yepi

This is, of course, relevant to return type void only.

Is there any way to make the implemntation match the interface?

Thanks,

yepi

推荐答案

你可以显式实现一个接口...但我不是100%确定语法,因为接口是通用的。



非通用接口,您只需删除访问说明符并在方法名称前添加接口名称和点:

You can explicitly implement an interface... but I am not 100% sure of the syntax since the interface is generic.

For non generic interface, you simply remove access specifier and add the name of the interface and a dot before the method name:
void InterfaceName.MethodName(...)





我不确定编译器是否还会抱怨。我不这么认为......



I am not sure if the compiler would still complain. I don't think so...


您无法更改所需方法的签名 - 并且返回类型是该签名的一部分。如果你这样做,那么它就不再实现接口了,所以它不会编译。

你也不能添加一个名称相同但返回类型不同的新方法,因为它无法分辨在某些情况下要调用哪种方法:

You can't change the signature of a required method - and the return type is part of that signature. If you do, then it no longer implements the Interface so it won't compile.
You also can't add a new method with the same name but a different return type as it becomes impossible to tell which of the methods to call in some circumstances:
            public void Add(T item)
                {
                ...
                }
            public bool Add(T item)
                {
                ...
                }
...
Add(new Item);

你打算用哪个添加

您唯一的选择是创建一个具有不同名称的新方法:

Which Add would you be calling?
Your only option is to create a new method with a different name:

public void Add(T item)
    {
    ...
    }
public bool AddIfPossible(T item)
    {
    ...
    return true;
    }


显而易见的解决方案(由OriginalGriff )是:

The obvious solution (as sugested by OriginalGriff) is:
public class My<T>: IList<T>
{
   public bool AddItem(T item) {...}
   public void Add(T item)
   {
      AddItem(item); //Extra call just to keep the interface happy
   }
}



这个解决方案有点大,因为它有两种使用方式的功能


This solution is a bit massy since it has two ways of using functionality of

void Add(T item)
i.e.
Add(item); and
AddItem(item);
with the former (the one that implements the interface!) being less efficient.



我正在寻找一个更干净的。



注意'void'是一种特殊的返回类型:你没有在方法中指定return void,而且我不是在寻找一般的返回类型协方差,只是避免针对[未指定] void检查结果类型的方法,即将void返回类型视为匹配任何类型而不是仅匹配void。



谢谢

yepi。


and I was looking for a "cleaner" one.

Note that 'void' is a special return type: you don't specify "return void" in the method, and I'm not looking for a general return type covariance, just a way to avoid checking result type against the [unspecified] void, i.e. regard void return type as "match any type" rather than "match void only".

thanks
yepi.


这篇关于如何实现具有返回值而非void的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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