<T> generic, broad type

In fact, as early as in 1999 JSR 14 specification mentions the generic concept, know jdk5 use of generics was formally released, after jdk7, and optimized for generics, generic inference.

Generic class

public class Pair<T> {
    private T first;
    private T second;

    public Pair() {
        first = null;
        second = null;
    }

    public Pair(T first, T second) {
        this.second = second;
        this.first = first;
    }

    public void setFirst(T newValue) {
        first = newValue;
    }

    public void setSecond(T newValue) {
        second = newValue;
    }
}

Defined appreciated how generic class First class name in the dorsal root <T> T this is any type. In Java libraries, using the variable E represents the set of element types, K and V respectively represent the type keywords and values ​​table . T (requires also the use of near letter U and S) means "any type." Then in the class members can use this T, you can turn your T as a parameter, you can put T as a return value can also type T as a member variable. What type T in the end of this storage depends on you specific examples of the type specified at the time of the Pair. but more than writing, once you specify an actual type, then this class in all T will be the same type.

public class Main {
    
    public static void main(String[] args) {
        Pair<String> pair = new Pair<>();
        pair.setFirst("第一");
        pair.setSecond("第二");
    }
}

You can also define multiple generic in a generic class

public class Pair<T,U> {
    private T first;
    private U second;

    public Pair() {
        first = null;
        second = null;
    }

    public Pair(T first, U second) {
        this.second = second;
        this.first = first;
    }

    public void setFirst(T newValue) {
        first = newValue;
    }

    public void setSecond(U newValue) {
        second = newValue;
    }
}

But you need to remember, because the generic scope at the class level. What is wrong wording.

 

 

 You define a T says you instantiate each class must specify a type, now, now you're trying to do to instantiate the class, while the direct use of T, then the T where you want it defined on the class ? Remember to use generics, first determine the specific type of generics.

Generic method

public class Demo3 {
    public   <T> void show(T t){
        System.out.println(t.toString());
    }
    public static  <S> void show2(S s){
        System.out.println(s);
    }
public class Main {
    public static void main(String[] args) {
        Demo3 demo3 = new Demo3();
        demo3.<String>show("a");
        demo3.show("a");
        Demo3.show2(1);
    }
}

You specify a generic in a way that is generic scope on a method body, that is to say, every time you call the method should specify a particular type of course you do not have to use <T> syntax for each call, because the generic inference jdk7 compiler naturally through your actual argument inferred type you want. to define a generic method, generic parameter T can be used, the method thereof, the return value.

Of course, when you specify the generic, you may also have the following wording

public class Demo1 {
    public  <String> void add(String t){
    }
}

But this is usually no sense, otherwise, what you want to express it? Defines a generic method, and defines the actual type of generic String?

Generic erase

Guess you like

Origin www.cnblogs.com/zumengjie/p/11595554.html