2016-07-19 24 views
1

嗨,我需要一些帮助理解这些代码。这些来自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(); 

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

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

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

回答

1

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

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

你问题的最后部分是新的;泛型类型参数可能会受到限制。这些声明的where部分对泛型类型参数可以作出限制;对于Get方法,如果类型实现IXeroResponse,则可以使用任何TResult(TResult是类型名称的占位符)。 new()意味着您用于TResponse的类型还必须具有公共缺省(无参数)构造函数。

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

更多约束泛型类型参数:https://msdn.microsoft.com/en-us/library/d5x73970.aspx

1

例如其中τ响应:IXeroResponse,新的()是指τ响应的类型必须为IXeroResponse和新的()意味着TResponse必须有无参数的公共构造函数。

一般格式为,其中T:MYCLASS,新的()装置T的类型必须是MyClass的和MyClass的必须有一个无参数的公共构造