Java泛型 - 这两个方法声明是否等价? [英] Java Generics - are these two method declarations equivalent?

查看:131
本文介绍了Java泛型 - 这两个方法声明是否等价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一些类 SomeBaseClass ,这两个方法声明是否相同?

  public< T extends SomeBaseClass> void myMethod(Class< T> clz)

  public void myMethod(Class< ;? extends SomeBaseClass> clz)


解决方案

对于调用者:是的,它们是等价的。



p>

区别在于,在第一个示例的代码中,可以使用类型T(例如,保存由 clz.newInstance() ),而第二个则不行。


Given some class SomeBaseClass, are these two method declarations equivalent?

public <T extends SomeBaseClass> void myMethod(Class<T> clz)

and

public void myMethod(Class<? extends SomeBaseClass> clz)

解决方案

For the caller: yes, they are equivalent.

For the code inside the method: no.

The difference is that within the code of the first example you can use the type T (for example to hold an object created by clz.newInstance()), while in the second you can't.

这篇关于Java泛型 - 这两个方法声明是否等价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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