Java 泛型 - 为什么是“扩展 T"?允许但不“实现 T"? [英] Java generics - why is "extends T" allowed but not "implements T"?

查看:25
本文介绍了Java 泛型 - 为什么是“扩展 T"?允许但不“实现 T"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 Java 中是否有特殊原因总是使用extends"而不是implements"来定义类型参数的边界.

I wonder if there is a special reason in Java for using always "extends" rather than "implements" for defining bounds of typeparameters.

示例:

public interface C {}
public class A<B implements C>{} 

被禁止但

public class A<B extends C>{} 

是正确的.这是什么原因?

is correct. What is the reason for that?

推荐答案

在类实现"或扩展"之间的通用约束语言中没有语义差异.约束的可能性是extends"和super"——也就是说,这个类是否可以赋值给另一个类(扩展),或者这个类可以从那个类赋值(超级).

There is no semantic difference in the generic constraint language between whether a class 'implements' or 'extends'. The constraint possibilities are 'extends' and 'super' - that is, is this class to operate with assignable to that other one (extends), or is this class assignable from that one (super).

这篇关于Java 泛型 - 为什么是“扩展 T"?允许但不“实现 T"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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