-
Notifications
You must be signed in to change notification settings - Fork 45.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
泛型之常用的通配符有哪些? #1692
Comments
感谢分享👍 |
感谢分享 👍 |
上面说的 T、E、K、V 其实都只是类型参数。而 ? 才是通配符,而且准确来说,是无限定通配符。
通配符分为有限定通配符,无限定通配符,超类型通配符。
有限定通配符:形式类似于 <? extends E>,E 如果为类,只有传入的类型参数为 E 的本身及子类才可以使用,E 如果为接口,则必须实现该接口,只能读不能写,可以用类型参数替代。
无限定通配符:形式为 <?>,都可以使用,只能读不能写,可以用类型参数替代。
超类型通配符:形式为 <? super E>,可以实现对象往父类型写入,不可以用类型参数替代。
简单总结
其中有限定通配符和无限定通配符只能读不能写,可以被类型参数替代。而超类型通配符可以使用对象往父类型写入,不可以用类型参数替代。
有限定通配符和无限定通配符既然能被类型参数替换,该如何选择使用的?
应该根据使用场景:
The text was updated successfully, but these errors were encountered: