1. What is a generic type?
Generics are suitable for many classes, which implement the concept of parameterized type parameter, which refers to the use of multiple types.
Polymorphism is a generalization mechanism.
For example, method A (base b) {}//base is a base class
This way, method A can accept any subclass from this base class base as a parameter. The parameters of a method can also be an interface, but because of the class's single-Inheritance system and interface constraints (
An interface still requires this your code works with that particular interface.
), it cannot be implemented to apply code to ambiguous types and cannot be loosely coupled.
2. When is the generic type used? How to use it?
1) Create a container class. Create container classes.
There is some cases where you want a container to hold multiple types of objects, but typically you only put one type of object into a container. One of the primary motivations for generics are to specify what type of object a container holds, and to has that specific Ation backed up by the compiler.
In some cases you want to use a container to hold multiple objects, but a container can usually store only one object. One of the main purposes of generics is to show what types of objects a container can hold, and to ensure accuracy by the compiler.
So we can not specify the type for the time being, then decide the concrete type later. Use the type parameter parameter, enclose it in <> angle brackets, put it behind the class name, and wait for the class to be replaced with an actual type, as in the following example,
public class Holder3<t> { private T A; Public Holder3 (T a) {this.a = A;} public void Set (T a) {this.a = A;} Public T get () {return A;} public static void Main (string[] args) {
holder3<automobile> h3 = New Holder3<automobile> (New Automobile ());
Automobile a = H3.get (); No cast needed //H3.set ("not an Automobile");//Error //H3.set (1);//Error
}
} ///:~
20180124. To be Continued ...
go to Java generics from Wikipedia
Https://zh.wikipedia.org/wiki/%E6%B3%9B%E5%9E%8B
Java generics [edit ] See also: Java
A Java generic parameter can only represent a class and cannot represent an individual object. Because the actual type of the Java generic type parameter is eliminated at compile time, the type of the type parameter cannot be learned at run time, and the base value type cannot be used directly as a generic type parameter. The Java compiler automatically joins the encoding of the type conversion when compiling generics, so the speed of operation is not accelerated by the use of generics.
Because the runtime eliminates generic object instance type information such as defects are often tseung sick, Java and JVM Development also try to solve this problem, such as Java by adding the Type deduction auxiliary information when generating bytecode, so that the reflection interface can obtain some of the generic information. By improving the implementation of generics in the JVM, it supports basic value type generics and direct access to generic information.
Java allows for a generic type of reference, including the following two forms [2] (Assumptions T
are generic type parameters, generic C
class, Universal, or generic type parameters): ------
T
Implements the interface I
.
T
Yes C
, or inherited from C
.
Java generics generic-thinking in Java reading notes