JAVA learning-generic class definition, generic method, generic interface, type wildcard, variable parameter

generic

It is a compile-time type safety detection mechanism that allows illegal types to be detected at compile time; it
is essentially a parameterized type, that is to say, the data type to be manipulated is specified as a parameter;

Generic class definition format

Modifier class class name <type>{ }
public class Generic{ }
T, E, K, V and other types of parameters are often used to represent generics;

Code example:
define a generic:

public class shili<T> {
    
    
    private T t;

    public T getT() {
    
    
        return t;
    }

    public void setT(T t) {
    
    
        this.t = t;
    }
}

Applied to a class:

public class fanxin {
    
    
    public static void main(String[] args) {
    
    
 
        shili<String> s1 = new shili<String>();
        s1.setT("hahaa");
        System.out.println(s1.getT());

        shili<Integer> s2 =new shili<Integer>();
        s2.setT(22);
        System.out.println(s2.getT());
    }
}

generic method

Generic methods can be used to define the situation that the same method requires various types of input content in a more convenient way when calling methods;
a method can be used to implement methods of multiple data types
Generic method example code:

   public class Gen<T> {
    
    
    public void show(T t){
    
    
        System.out.println(t);
    }
}

Implementation class code:

Gen<String> g1 = new Gen<String>();
        g1.show("hhh");
        Gen<Integer> g2 = new Gen<Integer>();
        g2.show(39);
        Gen<Boolean> g3 = new Gen<Boolean>();
        g3.show(true);

generic interface

Using the generic interface can define the situation that the same interface requires various types of input content in a more convenient way when calling the interface;

1. Define the interface modifier interface interface name <type>; example: public interface Generic{}
2. Define the implementation class of the interface, add Impl after the interface name for the name of the implementation class, and rewrite the method
public class GenericImpl implements Generic {}
3. Call in a polymorphic way in the application

        Generic<String> s = new GenericImpl<String>();
        s.show("haha");
        Generic<Integer> s1 = new GenericImpl<Integer>();
        s1.show(22);

Type wildcard <?>

<?> type wildcard

Its elements can match any type;
it just means that it is the parent class of various generic types, and elements cannot be added to it;

<?extends type> upper limit of type wildcard, upper limit is this type <?super type> lower limit of type wildcard, lower limit is this type

List<?> list1 = new ArrayList();

variable parameter

A method can realize the sum of multiple data
format: modifier return value type method name (data type...variable name)

     public static int sum(int... a){
    
    
        int sum = 0;
        for(int I : a){
    
    
            sum +=i;
        }
        return sum;
    }

Note: The variable here is actually an array.
If there are multiple parameters in a variable, including variable parameters, the variable parameters should be placed at the end

In the static methods of List, Set, and Arrays that use variable parameters, the returned collection cannot be added or deleted, that is, the length of the collection cannot be changed;

Guess you like

Origin blog.csdn.net/weixin_52723971/article/details/110748624