C# 模板参数作为模板接口
我正在尝试将这段代码从 Java 翻译为 C#,但在找到正确的 Syntax
Java:
public class MyClass<T extends IMyInterface<T>>
My attempts at Translation into C#:
public class MyClass<T, U> where T: IMyInterface<U>
If I do public class MyClass
,类声明处没有编译错误,但我不知道如何使用该类。
更具体地说,我有一个接口 IPoint
,它由类 EuclideanPoint:IPoint
实现。另外,我有一个模板类 Clusterer
,它不应该关心接口的模板参数,它应该只确保 U 是 IPoint 类型。
谢谢。
I am trying to translate this piece of code from Java to C# and I am having trouble finding the correct synthax
Java:
public class MyClass<T extends IMyInterface<T>>
My attempt at translating into C#:
public class MyClass<T, U> where T: IMyInterface<U>
If I do public class MyClass<T> where T: IMyInterface<T>
, there is no compile error at the class declaration, but I cannot figure out how to use the class.
More specifically, I have an interface IPoint<T>
, which is implemented by class EuclideanPoint:IPoint<EuclideanPoint>
. Also, I have a templated class Clusterer<U>
, which should not care about the template parameter of the interface, it should only make sure that U is of type IPoint.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,Java 的 C# 等效项只是:
Java 中一个类型参数,C# 中一个。
如何使用该类将取决于
IMyInterface
是什么以及它的实现方式。例如,如果是IEquatable
,则可以创建MyClass
,因为int
实现了IEquatable;
。No, the C# equivalent of the Java would just be:
One type parameter in the Java, one in the C#.
How you use the class will depend on what
IMyInterface<T>
is and what implements it. For example, if it wereIEquatable<T>
instead, you could create aMyClass<int>
becauseint
implementsIEquatable<int>
.看起来不错。
如果你有
那么你可以使用
looks good.
If you have
then you can use