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

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

问题描述

我想知道在Java中是否有一个特殊的原因:使用 extends 而不是 implements

示例:

  public interface C {} 
public class A< B实现了C> {}

p>

  public class A< B extends C> {} 

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

解决方案

在通用约束语言之间没有语义上的区别, 延伸。约束可能性是'extends'和'super' - 也就是说,这个类是可以分配给另一个(扩展的),还是这个类可以从那个分配(超级)。


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

Example:

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

is prohibited but

public class A<B extends C>{} 

is correct. What is the reason for that?

解决方案

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天全站免登陆