In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares to you is about the understanding of wildcards in Java generic. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Replacement principle
Combined with some of the object-oriented features of Java itself, it is easy to understand such a replacement principle:
A variable of a specified type can be assigned to any subclass of that type; a method that specifies a parameter of a type can be called by passing in a subclass of that type.
In general, this means that any type of variable we use can be replaced with a subtype of that type.
An incorrect inheritance relationship in generics
In generic programming, when we consider subtype relationships, it is easy to confuse a relationship and mistakenly adopt the principle of substitution.
For example:
List ints = new ArrayList (); ints.add (1); ints.add (2); List nums = ints; / / compile error
In this code, we see that the type parameter Integer is a subtype of Number, so it is easy to assume that List is also a subclass of List. Actually, it's not. This leads to type mismatches and compile-time errors.
At some point, we feel that one advantage of such a transformation does not seem to be to take advantage of the inheritance relationship between objects. If we could have a list that could handle both data of a certain type and all subtypes of that type, wouldn't it be possible to use both the benefits of generics and the benefits of object relationships? So here comes the wildcard (wildcard).
Wildcard character (Wildcard)
There is a method that uses wildcards in the Collection interface definition in the Java class library:
Interface Collection {... Public boolean addAll (Collection
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.