The generic generics on the static method can also be defined only in the static method, for example, in the definition and the use of generics in the custom support generics
ArrayList, if you want to write a
asArrayListmethod, you can specify an indefinite length argument to
ArrayList, it can be as follows:
PackageCc.OpenHome;
Public Class Util { Public Static <T> ArrayList<T>Aslist(T...A) { ArrayList<T> arrlt = new ArrayList<> (); for(T T : a) { arrlt. Add(t); } return arrlt; }
}
Want to use this
asList()method, the complete generic declaration language is as follows:
ArrayList<String> arrLt = Util.<String>asList("B", "X", "A", "M", "F", "W", "O");
In fact, the translator can
asList()To understand the model parameters.
TIt's actually.
Stringform, so it can be simplified and written as:
ArrayList<String> arrLt = Util.asList("B", "X", "A", "M", "F", "W", "O");
Some methods declare that the editor cannot push the actual form of the parameters from the arguments, and it may be pushed from other pipelines. For example, you might define the following:
...
public class BeanUtil {
public static <T> T getBean(Map<String, Object> data, String clzName)
throws Exception {
Class clz = Class.forName(clzName);
...略
return (T) bean;
}
}
Want to use the code in this program fragment
getBean()method, the full language method can be as follows:
Student student = BeanUtil.<Student>getBean(data, "cc.openhome.Student");
In the above fragment, the translator can
studentType of Mode
TOught to be
Student, so it can be simplified and written as:
Student student = BeanUtil.getBean(data, "cc.openhome.Student");
The translator will automatically push T the representation of the form, without additional designation <Student> , the full language is to be used in the chain operation. For example:
String name = BeanUtil.<Student>getBean(
data, "cc.openhome.Student").getName();
In the example above, if there is <Student> no designation, then there is no way to call back the Student object getName() method, because the translator will getBean() return the object of the form of the process Object , and the object will not have a getName() method, and thus the mistake, and this with the above Util asList()can compare:
Util.asList("B", "X", "A", "M", "F", "W", "O").get(10).toUpperCase();
This language is not going to be wrong, because the translator can asList() understand from the argument that the form is actually in the form T String , so the return form will be ArrayList<String> , and the call will be transferred get(10) back String , so the final call can be called toUpperCase() .
The type of the editor is very convenient to use, and in fact, you have been using the model to get the convenience, used in array copying
java.util.ArraysOf
copyOf()method, you do not actually use the cast language, for example:
String[] words = {"A", "X", "B", "Y"};
String[] newWords = Arrays.copyOf(words, words.length * 2);
java.util.ArraysOf
copyOf()Method can accept any type of array because it declares that it uses generics:
public static <T> T[] copyOf(T[] original, int newLength)
As a result, the editor can learn from the arguments that the form parameters are real, and the strong type pushes the language to be pronounced in the form of a change, which helps to simplify the language, especially after Java is introduced to Lambda in JDK8, which is very useful for the readable nature of the lambda language. You'll see the introduction later.
Generics on a Java static method