当前位置:网站首页>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

边栏推荐
- Pytorch distributed test pit summary
- Architecture evolution of high-performance servers -- Suggestions
- Leetcode123 timing of buying and selling stocks III
- Learning notes on February 18, 2022 (C language)
- [C language] implementation of magic square array (the most complete)
- QT pattern prompt box implementation
- Common dynamic memory errors
- Advertising effect cluster analysis (kmeans)
- Fishing detection software
- Weka download and installation
猜你喜欢

Postman usage notes, interface framework notes

One question per day,

How to download and install Weka package

QT source code online view

Shared memory synchronous encapsulation

google_ Breakpad crash detection

Install Kali extension 1: (kali resolution problem)

GDB debugging

QT excel table read / write library - qtxlsx

Stack and queue
随机推荐
Paddlepaddle paper reproduction course biggan learning experience
basic_ String mind map
Bessie's weight problem [01 backpack]
Dynamic memory allocation
QT file reading -qfile
(1) Introduction
RDB and AOF persistence of redis
Summary of regularization methods
The difference between sizeof and strlen
QT pop up open file dialog box QFileDialog
High precision addition
Summary of four parameter adjustment methods for machine learning
Review of arrays and pointers triggered by a topic
Qcodeeditor - QT based code editor
Character encoding minutes
55 specific ways to improve program design (1)
Function of getinstance() method
Qt: Pro project file
Source code analysis of synergetics and ntyco
Architecture evolution of high-performance servers -- Suggestions