java - 类有两个类型参数都有字符串和数字的上限 : Java

标签 java generics extends

Write the header for a generic class named MyType. The class should have two type parameters. The first type parameter’s upper bound should be the Number class. The second type parameter’s upper bound should be the String class.

这样看起来对吗?

public class MyType<T extends Number> {}

我将如何获得第二个类型参数作为上限?我可以做 extends Number extends String 吗?

最佳答案

你这样声明

 public class TestThis <T extends Number, X extends String> {}

x extends String 有效,请记住 String 是最终类,你不能扩展它。 Eclipse 给了我这个警告:

类型参数 X 不应受最终类型 String 的限制。 Final 类型无法进一步扩展

关于java - 类有两个类型参数都有字符串和数字的上限 : Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12649240/

相关文章:

java - 不知道为什么图像没有显示

java - TreeSet of String 对我来说不合逻辑

java - 检查泛型类的类型

java - 我如何阅读这个标题?

java - 如何用List<Child>调用方法,但方法的参数是List<Parent>?

java - 如何在运行时使用反射扩展类

java - 如何正确扩展这个抽象类?

java - 用瓷砖创建一个圆圈

java - Java 泛型中的精确类型匹配

java - 无法确定哪个 tomcat 过滤器会删除我们在过滤器链中的自定义 tomcat 过滤器注入(inject)到 httpservletresponse 中的数据