1. Wildcard, represents an unknown type, which represents a type that you don’t care about or cannot determine the actual operation. It is generally used in conjunction with container classes.
public void testV(List<?> list) {}
2, extends T>, define the upper limit, during which only reading ability is available. This approach indicates that the parameterized type may be the specified type or a subtype.
//t1要么是Test2,要么是Test2的子类 public void testC(Test1<? extends Test2> t1) { Test2 value = t1.getValue(); System.out.println("testC中的:" + value.getT()); }
3. super T>, lower limit definition, with reading ability and partial writing ability, subclasses can write to the parent class. This method indicates that the parameterized type can be a specified type or a parent class.
//t1要么是Test5,要么是Test5的父类 public void testB(Test1<? super Test5> t1) { //子类代替父类 Test2 value = (Test2) t1.getValue(); System.out.println(value.getT()); }
Note on the use of wildcards
The form of wildcard characters and type parameters are often used together.
The form of type parameters can replace wildcard characters.
People who can use wildcards use wildcards because wildcards tend to be simpler in form and more readable.
The above is the detailed content of How to use wildcards in Java?. For more information, please follow other related articles on the PHP Chinese website!