Java generics <> similarities and differences between various parameters and java
Let's talk about the main things involved in this article (refer to the Java programming idea book ):
1. Explains why List <Fruit> and List <Apple> are non-inherited.
2. List of questions raised by 1 <? Extends Fruit> is necessary.
3. description List <? Super Fruit> and List <? Differences between extends Fruit> and List <? Super Fruit> is necessary.
4. Description <? The difference between extends Fruit and <T extends Fruit>.
5. Native List and List <?> Difference.
6. Explain the custom generic class SelfBound <T extends SelfBound <T> {}.
The following three classes will be used:
class Fruit {}class Apple extends Fruit {}class Orange extends Fruit{}
1. Why is the non-inheritance relationship between List <Fruit> and List <Apple>.
I think the problem can be explained for the following two reasons:
1. Generics in Java are the features introduced later. To be compatible with previous code, generics have an erasure mechanism, list <Fruit> and List <Apple> are List in the erased class, and there is no inheritance relationship.
2. explain logically why there cannot be an inheritance relationship:
1 public void test(List<Fruit> list) {2 list.add(new Orange());3 }
In the code above, the test method receives a List of the <Fruit> type and inserts an Orange object in the list. This method is of no problem. Now, if there is an inheritance relationship between List <Fruit> and List <Apple>, this method can receive a List <Apple> type list parameter as a method parameter, however, an Orange object will be inserted in a List declared as list <Apple> In the method, which is obviously not logical. Therefore, the List <Fruit> and List <Apple> must be non-inherited.
Ii. List <? Extends Fruit> is necessary.
By introducing method 1, we can know that the test method can only accept List <Fruit> but not List <Apple> types. Now I want to write a method, what should I do if I can accept both List <Fruit> and List <Apple> lists? List <? Extends Fruit>.
List <? Extends Fruit> indicates that the object type held by this list is Fruit or the type (Apple or Orange) exported from Fruit, which is equivalent to setting an upper bound for the object type held by the List.
We only need to change the above test method:
1 public class TestGen {2 3 public void test (List <? Extends Fruit> list) {4/* because the input parameter is List <? Extends Fruit> parameter list, which cannot be added. 5 list. add (new Fruit (); 6 list. add (new Orange (); 7 list. add (new Apple (); */8} 9 10 public static void main (String [] args) {11 12 List <Apple> list = new ArrayList <Apple> (); 13 List <Orange> list1 = new ArrayList <Orange> (); 14 List <Fruit> list2 = new ArrayList <Fruit> (); 15 16 TestGen tg = new TestGen (); 17 18 tg. test (list); 19 tg. test (list1); 20 tg. test (list2); 21 22} 23}
Now the parameters in the test method are changed to List <? Extends Fruit> list. In the main method, you can see that this method can accept multiple types of List <Apple>, List <orange>, List <Fruit>, implemented the functions we wanted.
However, in the test method of the code above, we can also see that the list passed in as a parameter cannot be added, no matter what type of add, why?
It turns out that the input List type is List <? Extends Fruit> list. In the JDK source code, you can see that the generic parameter of the add method of List changes to <? Extends Fruit>, the compiler does not know which specific child type to add here, so it
The compiler rejects calls to methods that contain wildcards in the parameter list.
So how can we insert data to a list containing wildcard restrictions? In this case, we need to use <? Super Fruit> parameter.
Iii. List <? Super Fruit> and List <? Differences between extends Fruit> and List <? Super Fruit> is necessary.
As you can see, to insert an operation to a list containing wildcard restrictions, this generic restriction must be <? Super someClass>. As mentioned above, List <? Extends Fruit> specifies the object type that can be held by the List.
That is, the object can only be a child type of Fruit or Fruit. Correspondingly, List <? Super Fruit> specifies a lower bound for the object type that can be held by the List. That is, the object can only be a superclass of Fruit or Fruit.
Therefore, if you change the test method to the following, the add operation can be performed.
1 public void test(List<? super Fruit> list) {2 list.add(new Fruit());3 list.add(new Orange());4 list.add(new Apple());5 }
Because we can determine that the parameter passed in the test method is at least a List that holds the Fruit type, we can add Fruit and Fruit sub-type objects to this list without any problems.
4. <? The difference between extends Fruit and <T extends Fruit>.
The application scenarios of two things are different. <T extends Fruit> acts on methods or classes, and <? Extends Fruit> is not supported. The following is an example.
1 public class TestGen <T extends Fruit> {2 private T value; 3 4 public TestGen (T value) {5 this. value = value; 6} 7 8 public T getValue () {9 return value; 10} 11 12 public void setValue (T value) {13 this. value = value; 14} 15 16 public <E extends Fruit> void test1 (E) {17 System. out. println (e. getClass (). getName (); 18} 19 20 public static void main (String [] args) {21 22 TestGen <? Extends Fruit> tg = new TestGen <Fruit> (new Fruit (); 23 // because the setValue method parameter list contains wildcards (T in the setValue method is equivalent? Extends Fruit), The setValue method cannot be called. 24 // tg. setValue (new Fruit (); 25 26 tg. test1 (new Fruit (); 27 // tg. test1 (new Object (); the Object is not a child type of Fruit and cannot be passed into the test1 method as a parameter. 28} 29}
In the code, we can see that the limitation on the class <T extends Fruit> and the limitation on the method <E extends Fruit> make the acceptable types of the class and method both Fruit and Fruit sub-types.
5. Native List and List <?> Difference.
Unbounded wildcard? It seems equivalent to the native type, but the unbounded wildcard can have the following meanings: "I want to write this code using the Java generic type. I am not using the native type here, however, in this case, generic parameters can hold any type ".
List with unbounded wildcards <?> It is safer than native List. In the native list, We can insert objects, Fruit, and Apple objects to the same list at will, while List <?> The add operation is not allowed.
6. Explain custom generic class A <T extends A <T >> {}.
In Java generics, this is A common usage, which means that the base class (Class A here) uses its export class to replace its parameters, the generic base class becomes a common template for all its export classes. However, these functions use the export type for all its parameters and return values.
The following is an example.
1 class A <T extends A <T> {2 void set (T arg) {3 System. out. println ("in "); 4} 5} 6 // The generic base class is changed to a common public-energy template of all its export classes. 7 // The set Method of the base class can be called for both class B and class C., only 8 class B extends A <B> {9 10} 11 class C extends A <C> {12 13} 14 public class TestGen {15 void test2 (B b1, B b2, C c1, A a1) {16 b1.set (b2); 17 // when using A self-defined generic type, the set method called in the export class only accepts the corresponding export type, and does not accept the base type and other export types 18 // b1.set (c1 ); do not accept other export types 19 // b1.set (a1); do not accept the base type, the compiler cannot identify the attempt to pass the type to the set, because no method has such 20 // signature, in fact, this parameter has been overwritten 21} 22}