具有通用返回类的 Java 方法签名
以下2个方法签名有什么区别:
public <T extends MyClass> Set<T> getMyList() {...}
public Set<? extends MyClass> getMyList() {...}
What is the difference between the following 2 method signature:
public <T extends MyClass> Set<T> getMyList() {...}
public Set<? extends MyClass> getMyList() {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一个方法中,
T
可以在方法体中访问,而在第二个方法中则不能。这是否有用取决于该方法的工作。In the first one,
T
is accessible in method body, while in the second one it is not. Whether that's useful depends on the method's job.对于第一个,您可以将其用作:
对于第二个,您受到更多限制:
您希望如何实现第一个?第二个不好,因为它强制客户端代码使用通配符。
For first one you can use it as:
For the second, you are more limited:
How would you expect to implement the first? The second is bad because it forces client code to use wildcards.