当前位置:网站首页>Understanding generics mechanism

Understanding generics mechanism

2022-06-21 06:49:00 naoguaziteng

    Objective     record    

1. Generic mechanism

2. The syntax of generics  

3. Note on generics

4. Generic benefits : The problem can be compiled ahead of time , Generics have good extensibility and can avoid downward transformation .

5. Generics can be used in class , Interface , On the way

6. Generic wildcard


1. Generic mechanism

The generic mechanism is a way to explicitly work with data types , Defer until you create an object or call a method , Just to clear a mechanism , That is, the data type of data is known only when an object is created or a method is called . And the generic mechanism is JDK1.5 Mechanism only after version !

2. The syntax of generics   

<E,Y> among E Y It's the code , Generally refers to the reference data type .

3. Note on generics  

Generics are only valid at compile time , At run time, generics are erased . That is, the function of generics is only valid at compile time .

4. Generic benefits : The problem can be compiled ahead of time , Generics have good extensibility and can avoid downward transformation . 

Generally speaking , Multiple data types are not stored in the container , Only the same data type is stored in the container . The intuitive feeling of using generics is that you can limit the data types of the elements stored in the collection , Only generic types are allowed .

ArrayList<String> list2 = new ArrayList<String>();
        list2.add("abc");
        list2.add("abc");
        list2.add("hello");
        list2.add("world");
        // At this time, only String Type of data put into list2 Collection 

If there is a requirement that you want to store multiple reference data types in the collection and write generics ! You have to define generics as <Object>

ArrayList<Object> list3 = new ArrayList<>();
        list3.add(200);
        list3.add("abc");

5. Generics can be used in class , Interface , On the way  

Generic classes :   public class Class name < The generic type 1,…>

public class MyObject<X, Y> {

    private X x;
    private Y name;
    private Date birthday;

    public X getX() {
        return x;
    }

    public void setX(X x) {
        this.x = x;
    }

    public Y getName() {
        return name;
    }

    public void setName(Y name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

Generic interface :   public interface The interface name < The generic type >

public interface MyInterface<X> {
    public abstract void show(X x);
}

  When a class , To implement an interface , You can specify the specific data type of the generic defined on the generic interface here

public class AA implements MyInterface<Integer> {
    @Override
    public void show(Integer integer) {

    }
}

When a class implements a generic interface , I still don't want to be in a hurry to clarify the specific types of generics on this interface , So this class continues to be designed as a generic class

public class BB<X> implements MyInterface<X> {

    @Override
    public void show(X x) {

    }
}

Generic methods :   public < The generic type > Return type Method name ( The generic type Variable name )

public class MyCalc {

    public <P> void show(P p) {
        System.out.println(p);
    }
}

6. Generic wildcard <?> 

Generic wildcards refer to any type , If it's not clear , So that is Object And whatever Java The class . It is more arbitrary than Object To any degree !

  As shown in the figure above :Object A generic can only be followed by <Object type >, Other String Or custom Animal No type . But wildcards are OK , It can also include Object Generic .

There are two kinds of generic types that can be followed by wildcards , If you are not satisfied with the situation, you will report an error !

  • ? extends E:          Down limit , You can only write E Class and its subclasses
  • ? super E:              Limit upward , You can only write E Class and its parent class

import java.util.ArrayList;

public class FanXingTest {
    public static void main(String[] args) {
     
        // Limit upward   It can only be said  Animal Itself or Animal Parent class of 

        ArrayList<? super Animal> objects4 = new ArrayList<Animal>();
        ArrayList<? super Animal> objects5 = new ArrayList<Object>();


        // Down limit   It can only be said  Animal Itself or Animal Subclasses of 

        ArrayList<? extends Animal> objects7 = new ArrayList<Dog>();
        ArrayList<? extends Animal> objects8 = new ArrayList<Cat>();
        ArrayList<? extends Animal> objects9 = new ArrayList<Animal>();

    }
}


class Animal {
}
class Cat extends Animal {
}
class Dog extends Animal {
}

( Xiaobian is also trying to learn more ! I'll share it later !)

Hope to be helpful to friends !!!!

原网站

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