https://www.jianshu.com/p/66bd729bc129
< extends t> upper bound, indicated? is an unknown subclass of T.
<? Super T> Lower bound, expressed? is an unknown parent class for T.
Let's talk about this separately.
1. <? Extends t> Upper Limit Pass
Over here? Represents an unknown class, and T is a concrete class that, when actually used, needs to be replaced by a specific class that represents the subclass of the generic parameter when instantiated.
As an example,
We have a fruit class
/** * 水果类 */public abstract class Fruit { public abstract void eat();}
Then create two fruits to inherit the fruit class fruit
/** * Apple class */ Public class apple extends fruit { @Override public void eat () {System.out.println ( "I am an apple, I am sour and sweet "); }}
/** * 香蕉类 */public class Banana extends Fruit { @Override public void eat() { System.out.println("我是香蕉,我是软软的"); }}
We still need a melon eater.
/** * 吃瓜群众 */public class People<T extends Fruit> { public void eatFruit(T t){ t.eat(); }}
Note that when defining the people class, we used a generic upper-bound representation of <t extends fruit>
Let's create a class test
public class Test { public static void main(String[] args) { People<Apple> p1 = new People<>(); p1.eatFruit(new Apple()); People<Banana> p2 = new People<>(); p2.eatFruit(new Banana()); }}
Output result. png
If we create an orange class at this point, but do not inherit from the fruit parent class
/** * 橙子类 */public class Orange { public void eat() { System.out.println("我是橙子,我是酸酸的"); }}
Compile time will be error, indicating Orange is not in range, should inherit fruit class
Compile error. PNG 2. <? Super t> Low Limit Pass
Over here? Represents an unknown class, and T is a concrete class that, when used in practice, needs to be replaced by a specific class that represents the parent class of the generic parameter when instantiated.
For example
public static void addNumbers(List<? super Integer> list) { for (int i = 1; i <= 10; i++) { list.add(i); }}
This means that when we call this method we can pass in an integer or integer parent class, for example:
PublicClasstestlowerbounded {PublicStaticvoidAddNumbers(LIST<?Super Integer> list) {for (int i = 1; I <= 10; i++) {list.add (i);}} public static void main (string[] args) {list<integer> List1 = new arraylist<> (); AddNumbers (List1); list<number> list2 = new arraylist<> (); AddNumbers (LIST2); list<object> list3 = new arraylist<> (); AddNumbers (LIST3); list<string> list4 = new arraylist<> (); //compilation error because string is not the parent of integer //addnumbers (LIST4);}}
Both number and object are the parent of the Integer, so the compilation passes, and the string is not the parent of the integer, compiling an error.
Above is the generic upper bound wildcard character and the lower limit of the role of the wildcard, the knowledge point is relatively small, hope can accumulate 1.1 points, and we progress together.
Java generic wildcard upper and lower limit of wildcard characters (finishing)