Multpile继承和不同类型的方法。这种方法和继承意味着什么?

时间:2016-07-19 00:00:07

标签: oop c#-4.0 methods multiple-inheritance xero-api

您好我需要一些帮助来理解这些代码。这些来自Xero Api,遗憾的是没有评论,所以我很难理解下面的代码。

    public abstract class XeroReadEndpoint<T, TResult, TResponse> : IXeroReadEndpoint<T, TResult, TResponse> 
                where T : XeroReadEndpoint<T, TResult, TResponse>
                where TResponse : IXeroResponse<TResult>, new()

    public interface IXeroUpdateEndpoint<T, TResult, TRequest, TResponse>
                : IXeroCreateEndpoint<T, TResult, TRequest, TResponse>
                where T : XeroReadEndpoint<T, TResult, TResponse>
                where TResponse : IXeroResponse<TResult>, new()
                where TRequest : IXeroRequest<TResult>, new()

public IEnumerable<TResult> Delete<TResult, TResponse>(string endPoint) where TResponse : IXeroResponse<TResult>, new();

public IEnumerable<TResult> Put<TResult, TResponse>(string endPoint, object data) where TResponse : IXeroResponse<TResult>, new();

public IEnumerable<TResult> Get<TResult, TResponse>(string endPoint) where TResponse : IXeroResponse<TResult>, new();

我理解继承和面向对象编程的概念。但我对界面和抽象类中的代码感到困惑。

我也在努力理解以下三种方法。我得到了返回类型,但&lt;&gt;就在方法名称之后。在所有情况下,new()意味着什么。

有人可以告诉上述代码的实际含义。 感谢

2 个答案:

答案 0 :(得分:1)

首先,C#中没有多重继承。你所看到的是多接口实现,这意味着代码将公开相同的接口契约,但不共享任何实现。

尖括号中包含的类型称为泛型类型参数。最简单的解释方法是使用IList和IList接口。 IList是一个接口,其中实现使用列表语义存储对象(即,它是一组有序的对象)。问题是您存储在其中的任何内容都被强制转换为对象,因此您可以在IList实例中将System.String存储在第一个位置,将System.Int32存储在第二个位置。如果您只想要一个字符串列表,那么编译器或IList没有帮助。泛型解决了这个问题; IList可能只包含String类型,编译器会强制执行此操作,您可以确保只从IList实例中获取字符串实例。

你问题的最后一部分是新的;泛型类型参数可以是有限的。这些声明的where部分限制了泛型类型参数的含义;对于Get方法,您可以使用任何TResult(TResult是类型名称的占位符),前提是该类型实现IXeroResponse。 new()意味着您用于TResponse的类型还必须具有公共默认(无参数)构造函数。

您可以在MSDN上阅读有关泛型的更多信息:https://msdn.microsoft.com/en-us/library/512aeb7t.aspx

有关限制泛型类型参数的更多信息:https://msdn.microsoft.com/en-us/library/d5x73970.aspx

答案 1 :(得分:1)

例如,TResponse:IXeroResponse,new()表示TResponse必须是IXeroResponse类型,new()表示TResponse必须具有无参数公共构造函数。

通常格式为,其中T:Myclass,new()表示T必须是MyClass类型,MyClass必须有一个无参数公共构造函数

相关问题