The original wildcard-1,-1
Generic
Highlights of this section:
- 1. A generic class is a class with one or more types of parameters.
- 2 generic methods are methods with type parameters.
- 3. You can request that the type parameter must be one or more types of child types.
- 4. The generic type remains unchanged. When S is a subclass of T, G <S> and G <T> are irrelevant.
- 5. Use the wildcard G <? Extends T> or G <> super T> you can develop a method to accept a generic instance with subclass or parent class parameters.
- 6. When generic classes and generic methods are compiled, type parameters are erased.
- 7 Type erasure imposes many restrictions on generics. In particular, you cannot instantiate generic classes or arrays, convert them to generic types, or throw a generic class object.
- 8 Class <T> is a generic Class, which is very useful because methods such as newInstance are declared as T-type values.
- 9 even if generic classes and methods are erased in virtual machines, you can find out how they declare them at runtime.
1. generic classes
/*** A generic class has one or more types of parameters. * @ author gaolei **/public class Entry <K, V> {private K key; private V value; public Entry (K key, V value) {this. key = key; this. value = value;} public K getKey () {return key;} public V getValue () {return value ;}}
As you can see, the type parameters K and V are specified in the angle brackets after the class name. In the definition of Class Members, they are used as instance variable types, method parameters, and return value types.
The object that constructs a generic class is, for example, Entry <String, Integer> entry = new Entry <> ("1", 2); equivalent to new Entry <String, integer> ("1", 2). Note that the type parameter cannot be instantiated using the basic data type.
2. Generic Method
/*** @ Author GAOLEI **/public class Arrays {/*** the generic method is a method with type parameters. The generic method can be a common or generic class * @ param arr array * @ param I subscript I * @ param j subscript j * @ param <T> type T */public static <T> void swap (T [] arr, int I, int j) {T temp = arr [I]; arr [I] = arr [j]; arr [j] = temp ;}}
The swap method can exchange elements in any array (as long as the element type in the array is not the basic data type ),
Public static void main (String [] args) {String [] arr = {"1", "2"}; // <String> not required Arrays. <String> swap (arr, 0, 1); for (String s: arr) {System. out. println (s );}}
Through the above example, do you already feel that generics are so useful.
------------------------ Digress --------------------------------
Picture Source Network
Don't say love, don't lie, just like it.
Don't say hate, don't get bored, don't pretend to sigh
Just think that I am too troublesome to keep myself hurt
I told myself that my feelings are like this.
Why is it crazy?
A hug can never last long.
Are you sure you have any losses?
Forget it. I don't pay enough.
Every time I hear this story, I am scattered, and my province will always go through a wave of waves for a long time. I don't know how they have been missed in those years. I think it's still naive to think about it when I was a child... Here, I hope that a lover can become a member of the family. If he fails to be together, he will be furious. Of course, the premise is that you have a girlfriend first.
The above content only represents the author's personal point of view. If you do not want to modify it
Copyright by men with the same wind