1. Introduction
-
泛型
:JDK5
is a feature introduced in , which provides a compile-time type safety detection mechanism, which allows illegal types to be detected at compile time. Its essence is a parameterized type, which means that the data type being manipulated is specified as a parameter . -
When it comes to parameters, the most familiar is to define a method with formal parameters, and then pass the actual parameters when calling the method. So how do you understand parameterized types? As the name implies, it is to parameterize the type from the original specific type, and then pass in the specific type when using/calling.
-
This parameter type can be used in classes, methods, and interfaces, which are called generic classes, generic methods, and generic interfaces, respectively.
-
泛型
Definition format:-
<类型>
: Specifies the format of a type, where the type can be regarded as a formal parameter. -
<类型1,类型2...>
: Specify the format of multiple types, and separate multiple types with commas. The types here can be regarded as formal parameters. -
In the future, the type given in the specific call can be regarded as an actual parameter, and the type of the actual parameter can only be a reference data type.
-
-
泛型
the benefits of:-
Moved runtime problems to compile time
-
Avoid casts
-
2. Generic class
-
泛型类
The definition format of:-
Format:
修饰符 class 类名<类型> { }
-
example:
public class Generic<T> { }
-
Here
T
can be written as any identifier, and commonT、E、K、 V
parameters such as and are often used to represent generics.
-
-
Define a public generic class
Generic.java
public class Generic<T> { // 定义私有属性 private T t; // 重写 get 方法 public T getT() { return t; } // 重写 set 方法 public void setT(T t) { this.t = t; } } 复制代码
-
main
When used in a function, specify the generic type asString
, when using a generic class, all generic types point to thisString
.public class test { public static void main(String[] args) { // 新建对象 Generic<String> g1 = new Generic<String>(); // 使用 set 方法 g1.setT("dzm"); } } 复制代码
3. Generic method
-
泛型方法
The definition format of:-
Format:
修饰符 <类型> 返回值类型 方法名(类型量名){ }
-
example:
public <T> void show(T t){ }
-
-
Case:
public class test { public static void main(String[] args) { // 使用泛型方法 DZMLog("dzm"); DZMLog(88); DZMLog(true); } // 泛型方法 static <T> void DZMLog(T t) { System.out.println(t); } } 复制代码
output:
dzm 88 true 复制代码
4. Generic Interface Protocol
-
泛型接口协议
The definition format of:-
Format:
修饰符 interface 接口名<类型>{ }
-
example:
public interface Generic<T>{ }
-
-
define an
Generic.java
interface protocolpublic interface Generic<T> { // 接口方法定义 public default void show(T t) { System.out.println(t); } } 复制代码
-
Define a
GenericPro.java
class to conform to theGeneric.java
interface protocolpublic class GenericPro<T> implements Generic<T> { // 继承协议并实现协议方法 @Override public void show(T t) { // 实现协议方法 } } 复制代码
Five, type wildcard
-
In order to represent
List
the , you can use类型通配符
-
类型通配符
The definition format of :-
Type wildcard:
<?>
-
List<?>
: Indicates that the element type is unknown,List
and its elements can be matched任何的类型
. -
This wildcard
List
only indicates that it isList
the parent class of various generics, and cannot add elements to it.
-
-
通配符的
上下限
:-
如果说我们不希望
List<?>
是任何泛型List
的父类,只希望它代表某一类泛型List
的父类,可以使用类型通配符的上限。 -
类型通配符 - 上限
:<? extends 类型>
-
范例
List<? extends Number>
:它表示的类型是Number或者其子类型
,一定不能是父类型
。 -
类型通配符 - 下限
:<? super 类型>
-
范例
List<? super Number>
:它表示的类型是Number或者其父类型
,一定不能是子类型
。
-
-
案例:
import java.util.List; import java.util.ArrayList; public class test { public static void main(String[] args) { // 类型通配符: <?> List<?> list1 = new ArrayList<String>(); List<?> list2 = new ArrayList<Number>(); List<?> list3 = new ArrayList<Integer>(); // 类型通配符 上线: <? extends 类型> List<? extends Number> list5 = new ArrayList<String>(); // 这个会报错,因为 String 不属于 Number或者其子类型 List<? extends Number> list6 = new ArrayList<Number>(); List<? extends Number> list7 = new ArrayList<Integer>(); // 类型通配符 下线: <? super 类型> List<? super Number> list5 = new ArrayList<String>(); // 这个会报错,因为 String 不属于 Number或者其父类型 List<? super Number> list6 = new ArrayList<Number>(); List<? super Number> list7 = new ArrayList<Integer>(); // 这个会报错,因为 Integer 不属于 Number或者其父类型 } } 复制代码