Java中的类的双泛型约束:extends ConcreteClass&一世 [英] Double generic constraint on class in Java: extends ConcreteClass & I

查看:972
本文介绍了Java中的类的双泛型约束:extends ConcreteClass&一世的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Java中定义类似于以下C#通用约束的泛型约束?

Is there a way to define a generic constraint in Java which would be analogous to the following C# generic constratint ?

class Class1<I,T> where I : Interface1, Class2 : I

我想这样做:

class Class1<I extends Interface1, T extends I & Class2>

但是编译器抱怨Class2部分:类型参数不能后跟其他边界。 / p>

But the compiler complains about the "Class2" part: Type parameter cannot be followed by other bounds.

推荐答案

我可以看到解析Java代码的最简单的方法是让Class2成为一个接口。

The simplest way I can see of resolving the Java code is to make Class2 an interface.

您不能限制类型参数以扩展多个类或类型参数。此外,您不能在这里使用超级。

You cannot constrain a type parameter to extends more than one class or type parameter. Further, you can't use super here.

这篇关于Java中的类的双泛型约束:extends ConcreteClass&amp;一世的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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