[英]C# public class where T : class, Class, new() confusion
I am new to C# and I am faced with a class with this structure:我是 C# 的新手,我面临着一个具有这种结构的类:
public class SimpleGetter<TSubs> : GetterBase<TSubs>, ISubscriptionsSingleGetter<TSubs>
where TSubs : class, ISimpleSubscription, new()
{
UserSubscriptionsResponse<TSubs> ISubscriptionsSingleGetter<TSubs>.Get()
{
return ((ISubscriptionsSingleGetter<TSubs>)this).Get(null);
}
UserSubscriptionsResponse<TSubs> ISubscriptionsSingleGetter<TSubs>.Get(string userId)
{
return GetSubsResponse(userId);
}
}
I need to pass userID to the get() function (if possible), but I am confused on how to do that.我需要将 userID 传递给 get() 函数(如果可能),但我对如何做到这一点感到困惑。 I have tried to do some research on this but I do not even know what this way of defining a class is called.
我试图对此进行一些研究,但我什至不知道这种定义类的方式被称为什么。 I come from objective c where things seem more straight forward.
我来自objective c,那里的事情看起来更简单。
I do not even know what this way of defining a class is called
我什至不知道这种定义类的方式叫什么
This is a generic class.这是一个泛型类。
public class SimpleGetter<TSubs> : GetterBase<TSubs>, ISubscriptionsSingleGetter<TSubs>
where TSubs : class, ISimpleSubscription, new()
which has one generic type parameter TSubs
.它有一个泛型类型参数
TSubs
。 This class inherits the GetterBase<TSubs>
and implements the interface ISubscriptionsSingleGetter<TSubs>
.该类继承了
GetterBase<TSubs>
并实现了ISubscriptionsSingleGetter<TSubs>
接口。 Furthermore, the TSubs
must be a reference type and must have a parameterless constructor, which implements the ISimpleSubscription
interface.此外,
TSubs
必须是引用类型,并且必须具有实现ISimpleSubscription
接口的无参数构造函数。
public class FakeSubs : ISimpleSubscription
{
public FakeSubs()
{
}
// Here you have to implement ISimpleSubscription.
// You could also define any properties, methods etc.
}
// Now you could use your generic class as below:
var simpleGetter = new SimpleGetter<FakeSubs>();
Having created the above instance, you can call the Get
method as Tewr , pointed out in his comment:创建了上述实例后,您可以将
Get
方法调用为Tewr ,在他的评论中指出:
var response = ((ISubscriptionsSingleGetter<FakeSubs>)simpleGetter).Get(42);
Just to complement Christos' answer and help you understand the syntax a bit better, let's break the class definition term by term.只是为了补充 Christos 的回答并帮助您更好地理解语法,让我们逐项打破类定义。
public
- visible to all callers.public
- 对所有呼叫者可见。
class
- a reference type (ie not a struct
).class
- 引用类型(即不是struct
)。
SimpleGetter<TSubs>
- the class name is SimpleGetter, and it is generic with respect to the parameter TSubs. SimpleGetter<TSubs>
- 类名是 SimpleGetter,它对于参数 TSubs 是通用的。
: GetterBase<TSubs>
- it inherits from a base class which is itself generic with respect to the parameter TSubs. : GetterBase<TSubs>
- 它继承自一个基类,该基类本身对于参数 TSubs 是通用的。
, ISubscriptionsSingleGetter<TSubs>
- and it also implements the generic interface ISubscriptionSingleGetter. , ISubscriptionsSingleGetter<TSubs>
- 并且它还实现了通用接口 ISubscriptionSingleGetter。
where TSubs:
- there are some constraints on the type which the generic parameter TSubs must be of. where TSubs:
- 对泛型参数 TSubs 必须属于的类型有一些限制。
class
- it must itself also be a reference type. class
- 它本身也必须是引用类型。
ISimpleSubscription
- it must implement this (non-generic) interface. ISimpleSubscription
- 它必须实现这个(非通用)接口。
new()
- it must have a public parameterless constructor. new()
- 它必须有一个公共的无参数构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.