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

 Insert picture description here

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

 Insert picture description here

原网站

版权声明
本文为[BlackPenguin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200502589615.html