快速构建网站,中东跨境电商平台有哪些,医疗器械生产许可证,刷赞网站推广免费软件做Java开发工作好多年了。今天偶然翻到 java.lang.TypeVariable的源码#xff0c;好奇为什么 TypeVariable.getBounds()返回类型是个数组。 一般不都是T extends Number 这样用码#xff1f;T难道还能extends多个类型#xff1f; 同问#xff1a;不应该是extend,为…做Java开发工作好多年了。今天偶然翻到 java.lang.TypeVariable的源码好奇为什么 TypeVariable.getBounds()返回类型是个数组。 一般不都是T extends Number 这样用码T难道还能extends多个类型 同问不应该是extend,为什么要加个s当复数 带着问题一通找最终找到了Oracle官方文档《Bounded Type Parameters》
才知道类型变量定义还有Multiple Bounds的概念,说明如下 如下示例ServiceD定义了A类型变量–A extends ClassAIfAIfB ,就要求A不仅要是ClassA的子类还要实现IfA和IfB接口
所以ClassC才能满足这个要求 public static class ClassA{}public static interface IfA{}public static interface IfB{}public static class ServiceDA extends ClassAIfAIfB {}public static class ClassC extends ClassA implements IfA,IfB{}public static class ServiceE extends ServiceDClassC{}终于明白为什么继承关键字是extends而不是extend原来用到这儿了。 真是又学到了以想想什么场景需要用到这样的定义。
参考资料
《Bounded Type Parameters》