当前位置:网站首页>Generic - learning notes
Generic - learning notes
2022-06-25 15:27:00 【BlackPenguin】
meaning
Generic , As the name suggests, it is “ Generic type ”, That is not a specific type . You can use generics to refer to multiple specific types , Is a parameterized type . Generic Valid only at compile time , The operation phase has no effect , After compilation, you will take De genericization
operation .
Generic types can logically be seen as many different types , But the essence is the same data type .
It's just that when we write code , Sometimes do not know the class 、 Method 、 What specific types of data does the interface need (String、Long、Integer… ?), Sometimes a class or method or interface may have multiple instances of different types . So use generics to refer to a type , It is convenient for us to write code . I know from here , Generics are only valid at compile time , That is, you need to pass a specific type after instantiation , After compilation, the specific type will be passed to the method or class or interface using generics .
Generic wildcard
Borderless wildcard
Use <?>
Represents unbounded generic wildcards , Indicates that it can be any type of List, You can also create your own type . But when instantiating, you must give the specific type .
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("good");
list.add("first");
loop(list);
}
public static void loop(List<?> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
Upper boundary wildcard
Use <? extends ...>
Represents a generic wildcard with an upper boundary , Represents the generic Refers to the upper limit of the type , It can be a subclass or grandchild of this class . For example, the following code is a code whose upper boundary is Number
Generic wildcard for class , It can only be instantiated with Number
A subclass or grandchild of , because String Class is not his descendant class , therefore String Kind of complains .
publicstatic void main(String[] args) {
List<Double> list = new ArrayList<>();
list.add(12.3);
list.add(45.6);
loop(list);
}
public static void loop(List<? extends Number> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
Lower boundary wildcard
The lower boundary adopts <? super ...>
Express , Instantiation can only use this class or to Object Classes between . because Number
It's also Integer The parent of a class , So it can be used Number
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(45);
loop(list);
List<Number> list2 = new ArrayList<>();
list2.add(12.3);
list2.add(45.6);
loop(list2);
}
public static void loop(List<? super Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
The use of generics
Must declare first , Reuse . Generics can be used K V T E To express .
1. Generic classes
Defining generic classes can increase the flexibility of your code , Sometimes we may not know which parameters or types are required in the class , So use generic classes .
public class GenericClass <T>{
private T t;
public GenericClass(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
2. Generic methods
Means that methods can be defined using generics . You can use class defined generics , Or the type defined by the method itself .
Be careful : Static methods of a class cannot use generic types defined by the class , Because the generic type of a class only knows what specific type it is when it is instantiated , Static classes can be used without class instantiation , So the static class does not know what its corresponding specific type is after compilation , So it's going to go wrong .
public class GenericClass<K, V> {
// Methods can use generic types of classes
public K method1(K k, V v) {
return (K) null;
}
// You can use method defined generics , Be careful ,T Be sure to state
public <T> T method2(T t, V v) {
return (T) null;
}
// Be careful , A static method of a class cannot use a generic type of a class , The following usage will report an error
/* public static K method3() { return null; } */
// Amend it to read ,K Refers to a newly defined generic type , Override the generics of the class K, And defined in the class K Dissimilarity
public static <K> K method3() {
return null;
}
}
3. Generic interface
Use generics in interfaces to refer to specific types , More flexible .
public interface GenericInterface<T> {
T method1(T t1, T t2);
T method2(T t);
T method3(T t);
}
summary
边栏推荐
- Character encoding minutes
- Learning notes on February 5, 2022 (C language)
- Source code analysis of zeromq lockless queue
- Automatic correlation between QT signal and slot
- User defined data type - structure
- Errno perrno and strerrno
- Fishing detection software
- [untitled] PTA check password
- Getting started with lambda8 new features
- The robot is playing an old DOS based game
猜你喜欢
Agent and classloader
Several common optimization methods
Design and implementation of timer
Js- get the mouse coordinates and follow them
basic_ String mind map
Single user mode
[paper notes] street view change detection with deconvolutional networks
Install Kali extension 1: (kali resolution problem)
Kali SSH Remote Login
[C language] implementation of magic square array (the most complete)
随机推荐
Boost listening port server
[untitled] PTA check password
Record the time to read the file (the system cannot find the specified path)
The robot is playing an old DOS based game
System Verilog - data type
Install Kali extension 1: (kali resolution problem)
CPU over high diagnosis and troubleshooting
RDB and AOF persistence of redis
Learning notes on February 8, 2022 (C language)
CV pre training model set
Data feature analysis skills - correlation test
Architecture evolution of high-performance servers -- Suggestions
JS capture, target, bubble phase
Generation method and usage of coredump
Esp8266 building smart home system
Yolov5 Lite: fewer parameters, higher accuracy and faster detection speed
Basic syntax and common commands of R language
QT set process startup and self startup
Single user mode
55 specific ways to improve program design (1)