Turn from: https://segmentfault.com/a/1190000002646193
Generics are generated because of the creation of a container class
generic class
The container class should be one of the most reusable class libraries. Let's look at how a container class is defined without a generic type:
public class Container {
private String key;
private String value;
Public Container (string k, string v) {
key = k;
Value = V;
}
Public String Getkey () {return
key;
}
public void Setkey (String key) {
This.key = key;
}
Public String GetValue () {return
value;
}
public void SetValue (String value) {
this.value = value;
}
}
The container class holds a pair of key-value key values, but the type is dead, and if I want to create a key-value pair that is String-integer type, the current container cannot be done and must be customized. Then this obvious reusability is very low.
Of course, I can use object instead of string, and we can only do that before Java SE5, because object is a base class for all types, so it can be transformed directly. However, this flexibility is not enough, because it is still a specified type, only this time to specify a higher type level, it is not possible to specify a type. It is not possible to know what the specific type is at run time.
So, there is a generic type.
public class Container<k, v> {
private K key;
private V value;
Public Container (k K, v V) {
key = k;
Value = V;
}
Public K Getkey () {return
key;
}
public void Setkey (K key) {
This.key = key;
}
Public v. GetValue () {return
value;
}
public void SetValue (V value) {
this.value = value;
}
}
At compile time, it is impossible to know what type K and V are, and the memory is actually constructed and allocated according to the type only at run time. You can look at the container classes for different types of support now:
public class Main {public
static void Main (string[] args) {
container<string, string> c1 = new Container< ; String, string> ("name", "Findingsea");
container<string, integer> c2 = new container<string, integer> ("Age",);
Container<double, double> c3 = New container<double, double> (1.1, 2.2);
System.out.println (C1.getkey () + ":" + c1.getvalue ());
System.out.println (C2.getkey () + ":" + c2.getvalue ());
System.out.println (C3.getkey () + ":" + c3.getvalue ());
}
Output:
Name:findingsea
age:24
1.1:2.2
generic interface
In a generic interface, the builder is a good idea to look at the following builder interface definitions:
Public interface Generator<t> {public
T next ();
}
Then define a generator class to implement this interface:
public class Fruitgenerator implements generator<string> {
private string[] fruits = new string[]{"Apple", "Ban Ana "," Pear "};
@Override public
String Next () {
Random rand = new Random ();
Return Fruits[rand.nextint (3)];
}
Call:
public class Main {public
static void Main (string[] args) {
Fruitgenerator generator = new Fruitgenerator ();
System.out.println (Generator.next ());
System.out.println (Generator.next ());
System.out.println (Generator.next ());
System.out.println (Generator.next ());
}
Output:
Banana
Banana
Pear
Banana
generic Method
A basic principle is: whenever you can do it, you should try to use the generic method as much as possible. That is, if you use a generic method to override the generalization of the entire class, you should be limited to a generic method. Here's a simple definition of a generic method:
public class Main {public
static <T> void out (t) {
System.out.println (t);
}
public static void Main (string[] args) {out
("Findingsea");
Out (123);
Out (11.11);
Out (true);
}
You can see that the parameters of the method are completely generalized, this process involves the compiler type derivation and automatic packaging, also said that the original needs of our own type of judgment and processing, now the compiler to help us do. This makes it much more flexible to define the method without having to consider which types of parameters to deal with in the future.
Look at an example of a generic method and a variable parameter:
public class Main {public
static <T> void out (t ... args) {for
(t T:args) {
System.out.println (t);
}
}
public static void Main (string[] args) {out
("Findingsea", 123, 11.11, True);
}
The output is the same as the previous piece of code, and you can see that generics can be perfectly combined with variable parameters.
Where <T> is for canonical parameters; T represents the return value type.