Generics are an important feature of JAVA. Using generic programming, you can Code reuse rate is improved.
Implementation: Use wildcards in generic methods
Create a class: WildcardsTest.
Create a method getMiddle() for getting the middle value of a given list.
In generics, use "?" as a wildcard character. The use of wildcard characters is similar to that of ordinary type parameters. For example, wildcard characters can use the extends keyword to set the upper limit of the value. For example,
extends Number>
indicates that Byte, Double, Float, and Integer are suitable for this type parameter.
Also, there is an upper limit and a lower limit, such as
super Number>
w means that the type parameter is the parent of the Number class Class, such as Object.
package com.xiaoxuzhu; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Description: * * @author xiaoxuzhu * @version 1.0 * * <pre class="brush:php;toolbar:false"> * 修改记录: * 修改后版本 修改人 修改日期 修改内容 * 2022/5/10.1 xiaoxuzhu 2022/5/10 Create *
Generics
You can use
1. Parameter types are used in the creation of classes, generic classes.
2. Parameter types are used in the creation of interfaces, generic interfaces.
3. Parameter types are used in the creation of methods, generic methods. Note the declaration position of the generic
? Type wildcard character
Several forms of wildcard character:
1. Unlimited wildcard character, >.
2. Upper limit wildcard, extends User>. Indicates that the parameter type can only be a subclass of User.
3. Lower limit wildcard, super User>. Indicates that the parameter type can only be the parent class of User.
public class P<T> { private T t; public T getT(){ return t; } //通配符 //设置指定类型的范围,超过范围就会报错 //extends : 指定范围必须是其(这里是List)子类 public void set(P<? extends List> list){ return; } //super : 指定类型必须是其(这里是List)父类 public void setSuper(P<? super List> list){ return; } public static void main(String[] args) { new P<String>(); //T会被替换成String new P<Integer>(); //T会被替换成Integer Person<String,List,Double,User> person = new Person<String,List,Double,User>();//T会被替换成String,B会被替换成List... person.eat("鱼"); } } class User<T extends P>{//这里给类的泛型指定范围,T必须要继承类P。 } //一个类可以同时指定多个泛型 class Person<T,B,S,N>{ private T t; private B b; private S s; private N n; public void eat(T t){ this.t = t; System.out.println("我正在吃"+this.t); } }
The declaration of generics indicates that an unknown data type is to be used in the creation of classes, interfaces, and methods. It can be of type Integer or String. Just define its type as T, S or N, etc.
When instantiating, you must declare what type T is.
When defining the usage method of a generic object, it is not known what type T is. It may be a String type or an Integer type. If T is defined as a certain generic data type, the parameters can only be of this data type. At this time, wildcards are used instead of certain generic data types.
Use generics and wildcards to improve code reusability.
If an object is divided into two parts: declaration and use. Generics focus on code reuse in type declaration, while wildcards focus on code reuse in usage. Generics are used to define uncertainty about internal data types, and wildcards are used to define uncertainty about the type of objects used.
The above is the detailed content of How to use wildcards to enhance Java's generic performance. For more information, please follow other related articles on the PHP Chinese website!