Generic Supplements
Keynote Teacher: Wang Shaohua QQ Group: 483773664 Learning Goals
Mastering the difference between a generic method and a type wildcard
Generics and Method overloading
Generics and Arrays
The difference between a generic method and a type wildcard (a) Most of the time, you can use a generic method to replace the type wildcard character
For example, a requirement: Define a method, receive an arbitrary collection, and print out all the elements in the collection. Generic methods and type wildcards can all be implemented
12345678910111213 |
public class Need {
public <T>
void printCollection(Collection<T> c){
for (T t : c) {
System.out.println(t);
}
}
public void printCollection2(Collection<?> c){
for (Object object : c) {
System.out.println(object);
}
}
}
|
(b) Sometimes, we can use both generic methods and wildcard characters
12 |
public <T> void copy(List<T> dest,List<? extends T> src){ } |
Of course, the above method can also be changed into a generic method
123 |
public <T ,E extends T> void copy2(List<T> dest,List<E> src){ } |
(c) types can define variables
12345 |
public <T> void printCollection(Collection<T> c){ for (T t : c) { System.out.println(t); } } |
(iv) Set<e> only allow elements of type E,set<?> equals set<? extends Object> only elements cannot be placed
Add (E)
Ii. generic method and method overloading
Method Overloading: Method overloading refers to defining multiple methods with the same name in a class, but requiring the number of types or parameters for each method to have different parameters.
(i) First case: the upper and lower limits of the generic type
Because generics allow you to set the upper limit of the wildcard character
So the following two methods are method overloads?
12345 |
public <T> void copy(List<T> dest,List<? extends T> src){ } public <T> void copy(List<? super T> dest,List<T> src){ } |
Reason
12345678 |
public class Test { public static void main(String[] args) { List<Number> c = new ArrayList<Number>(); List<Integer> c1 = new ArrayList<Integer>(); Need need = new Need(); need.copy(c, c1); } } |
The compiler was unable to determine which method to call.
Of course, the above two methods at the time of compilation, it will not pass!
(ii) generic type specified differently
1234 |
public void method(List<String> list) { } public void method(List<Integer> list) { } |
This notation is incorrect and the compilation fails.
This is because, from the Java language level, the method overloads depend on the same method name, the number of arguments, the type, the order, and the list<integer> and list<string> types are list<e> after erasure; This does not conform to the requirements of method overloading.
Note:
In Jdk6 and below, the following code compilation will pass, JDK7, compilation will not pass, JDK8 compilation will also pass
1234567 |
public String test(List<String> list) { return "" ; } public int test(List<Integer> list) { return 1 ; } |
Iii. Generics and Arrays
(a), Java does not supportarray object of generic type1. You cannot create an array object that holds a generic type
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/80/3D/wKiom1c78qHjYAiXAAAZCivwpPk262.png " data_ue_src= "E:\My knowledge\temp\15936359-39fb-4629-b73d-562060c83b24.png" >
2. Correct definition method
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/80/3B/wKioL1c784uylavnAAAf2887VIU603.png " data_ue_src= "E:\My knowledge\temp\4dcc1ab8-0ba1-4a07-af89-bf167da0098f.png" >
3. Take value
<>
To learn from Mr. Wang. Generics (ix): generics-Supplements