Generic classes, generic methods and generic applications

Generic classes, generic methods and generic applications
Generics is a new feature of Java SE 1.5. The essence of generics is parameterized types, that is, the data type to be manipulated is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, which are called generic classes, generic interfaces, and generic methods, respectively. The advantage of introducing generics in the Java language is that it is safe and simple.

Before Java SE 1.5, in the absence of generics, the "arbitraryization" of parameters was achieved through a reference to the type Object. The disadvantage of "arbitraryization" is that explicit coercion is required, and this The conversion is performed under the condition that the developer can predict the actual parameter type. In the case of a forced type conversion error, the compiler may not prompt an error, and an exception occurs at runtime, which is a security risk.

The benefit of generics is that type safety is checked at compile time, and all casts are automatic and implicit to improve code reuse.

Mr. Ma Yang from Bluebridge School of Software explained the related content of generics:

Rule restrictions:
1. The same generic can correspond to multiple versions (because the parameter type is uncertain), and instances of generic classes of different versions are incompatible of.
2. Generic parameter types can use the extends statement, such as <T extends superclass>. It is customary to call it a "bounded type".
3. The parameter type of the generic can also be a wildcard type. For example, Class<?> classType = Class.forName("java.lang.String");
Generics also have interfaces, methods, etc., which have a lot of content. It takes a lot of effort to understand and master them and apply them proficiently. Here are two examples I wrote when I knew about generics (written according to the impression I saw), to achieve the same function, one used generics, the other did not use, through comparison, you can quickly learn generics Application, learning this basically learned 70% of the generic content.

Example 1: Using Generics


class Gen<T> {
    private T ob; // define generic member variable
 
    public Gen(T ob) {
        this.ob = ob;
    }
 
    public T getOb() {
        return ob;
    }
 
    public void setOb(T ob) {
        this .ob = ob;
    }
 
    public void showType() {
        System.out.println("The actual type of T is: " + ob.getClass().getName());
    }
}
 
public class GenDemo {
    public static void main(String [] args) {
        // define an Integer version of the generic class Gen
        <Integer> intOb = new Gen<Integer>(88);
        intOb.showType();
        int i = intOb.getOb();
        System.out.println("value= " + i);
        System.out.println("----------------------------- -----");
        // define a String version of the generic class Gen
        Gen<String> strOb = new Gen<String>("Hello Gen!");
        strOb.showType();
        String s = strOb.getOb ();
        System.out.println("value= " + s);
    }
}
Example 2: No generics are used
?


class Gen2 {
    private Object ob; // Define a generic type member
 
    public Gen2(Object ob) {
        this. ob = ob;
    }
 
    public Object getOb() {
        return ob;
    }
 
    public void setOb(Object ob) {
        this.ob = ob;
    }
 
    public void showTyep() {
        System.out.println("The actual type of T is: " + ob.getClass().getName());
    }
}
 
public class GenDemo2 {
    public static void main(String[] args) {
        / / Define an Integer version of class Gen2
        Gen2 intOb = new Gen2(new Integer(88));
        intOb.showTyep();
        int i = (Integer) intOb.getOb();
        System.out.println("value= " + i);
        System.out.println("---------------------------------");
        // define the class A String version of
        Gen2 Gen2 strOb = new Gen2("Hello Gen!");
        strOb.showTyep();
        String s = (String) strOb.getOb();
        System.out.println(" value= " + s);
    }
}
Running results:
The results of running the Demo in the two examples are the same, and the console output results are as follows:
The actual type of T is:
java.lang.Integer
value= 88
--------------- -------------------
The actual type of T is: java.lang.String
value= Hello Gen!
Process finished with exit code 0
to understand this, the basic generic type in the future App and code reading is no problem.


Introduction to Generics: 1.


Customizing Generic Interfaces, Generic Classes and Generic Methods

Parameters are often used to represent generic parameters, because they receive type arguments passed in when they are used externally. So for different incoming type arguments, is the type of the corresponding object instance generated the same?


1 public class GenericTest {
2
3 public static void main(String[] args) {
4
5 Box<String> name = new Box<String>("corn");
6 Box<Integer> age = new Box<Integer>( 712);
7
8 System.out.println("name class:" + name.getClass()); // com.qqyumidi.Box
9 System.out.println("age class:" + age.getClass()); // com .qqyumidi.Box
10 System.out.println(name.getClass() == age.getClass()); // true
11
12 }
13
14 }


The reason lies in the purpose of the concept of generics in Java , so that it only acts on the code compilation stage. [During the compilation process, after the generic result is correctly checked, the relevant information of the generic will be erased.] That is to say, the successfully compiled class file does not contain any generic information. Generic information does not enter the runtime phase.

This can be summed up in one sentence: Generic types are logically viewed as multiple different types, which are actually the same basic type.



2. Type wildcard

1. Can it be regarded as a generic type with parent-child relationship similar to Box<Number> and Box<Integer>?

public class GenericTest {
2
3 public static void main(String[] args) {
4
5 Box<Number> name = new Box<
6 Box<Integer> age = new Box<Integer>(712);
7
8 getData(name);
9        
10 //The method getData(Box<Number>) in the type GenericTest is
11 //not applicable for the arguments ( Box<Integer>)
12 getData(age); // 1
13
14 }
15    
16 public static void getData(Box<Number> data){
17 System.out.println("data :" + data.getData());
18 }
19

Obviously, through the prompt information, we know that Box<Number> cannot be regarded as the parent class of Box<Integer> logically.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326524150&siteId=291194637