JDK1.5 introduces several new extensions to the Java language, one of which is generics. You may have learned about generics in other languages, most notably C + + templates. Java generics and it have a lot of similarities, but there are also many important differences.
The most common place for Java generic applications is the various container types, the collection inheritance tree.
First, define generics: public interface list<e>{void Add (E x); Iterator<e> Iterator ();}
Generics can be either a class or an interface, or it can be a method.
Unlike the definition of a normal class or interface, the generic definition requires a formal type parameter (E), which is placed inside the angle brackets. This type of form can then be used in the definition body as if it were a normal type (with a certain limit, which will be said later).
Ii. generics and sub-types
Assuming that Foo inherits from Bar,g is a generic type, the g<foo> is not a subtype of g<bar>, although Foo is a subtype of Bar. This is contrary to our intuition and must be noted.
Three, wildcard characters?
1,? Indicates that the type parameter is indeterminate (unknown), which can be any type, such as Collection<?> is the parent class of any collection class, and the element that represents the collection can be of any type. For a collection of unknown types, we can take out elements and assign them to object objects, but cannot add objects of type object to this collection, such as collection<?> C = new arraylist<string> () ; Then C.add (new Object ()) is illegal.
2. Restricted wildcard characters, such as public void drawall (list<?). extends shape> shapes) {...},? The type represented is unknown, but there are limitations, only Shape or his subclasses, such as List<shape>, List<circle>, list<square>, etc. Note that we used the extends keyword, and we said that shape is a wildcard character? The upper bound.
Using wildcards gives you the flexibility and the cost of having to write to the shape above and read only. The following example is illegal, because? There may also be subclasses of rectangle.
Public void extends shape> shapes) { compile-time error! New Rectangle ());}
3. Nether Wildcard: Use super keyword
Public Static Super T> SNK) { ...}
Sink<object> s; Collection<string> CS;
yes!
Iv. generic methods
Write a generic way to add an array to the collection:
Error method:
static void fromarraytocollection (object[] A, collection<?> c) {for (Object o:a) {
compile-time error }}
The correct method: we call the following method, as long as the type of the element that satisfies the array is the type of the collection element or subclass it can be. Such as:
static <T> void fromarraytocollection (t[] A, collection<t> c) {
for (T o:a) {
C.add (o); Correct
}
}
When to use wildcards, when to use a generic method
Wildcards are designed to support flexible subtypes, and generic methods are used to describe the dependencies between a method's parameters and their return value types, and if there is no such dependency, do not use a generic method and you should use a wildcard character.
Of course, wildcard characters can also be used in tandem with generic methods, such as:
class Collections {public staticvoidextends t> src) { ...}
Java Generics (1)