Java generic Quik Start
Author (Kaedev) JDK1.5 makes us look for a long time, but when he is released, replace the version number 5.0. This shows that Java has greatly changed. This article will explain new features supported by JDK5.0 ---- Java's generics. 1.1. Java generic actually Java's generic is a class that uses type as a parameter. Like the method we write, the method is such a Method (String Str1, String Str2), the value of the parameters STR1 and STR2 in the method is variable. The generic is also the same, which writes Class Java_Generics
System.out.println (s);}} Correct output: Value is just an example (the collection framework in Java is generic, and it costs 2 times here. "It is time to see if it is created as a parameter. The class, the parameter is K, V, the incoming "value" is a String type. He does not have a specific type of payment. We used to define a class. In the input input parameters, it is fixed, but what is otherwise requested, but now write the program, it can not set the type of parameters, specifically It is time to determine, adding the versatility of the program, like a template. Oh, a template like C (similar).
1.1.1. Wild Wildcaries Let's take a look at these procedures first:
// Code List 2 Void TestGen0Medthod1 (List L) {for (Object O: L) System.out.Println (O);} Take a look at this method, this method will be compiled, if you pass String, That is, List
// code list 3 void TestGen0Medthod1 (List
// code list 4 list
1.1.2. Writing generic classes Note: 1. When defining a generic class, define the form type parameters between "<>", for example: "Class Testgen
// Code List 5
Import java.util.hashtable; Class test {public static void main (string [] args) {hashtable h = new hashtable (); h.put ("key", "value"); string s = (string) h. GET ("key"); system.out.println (s);}}
This type of conversion is very annoying, and forced type conversion will bring potential dangers, and the system may throw a classcastexception exception information. In JDK5.0, we can do this, such as:
// code list 6 Import java.util.hashtable; class test {public static void main (string [] args) {hashtable
1.2.2. Automatic unpacking and automatic packaging features have seen a bit awkward from above, H.Get (New Integer (123)) here New Integer (123); soon, before JDK5.0 we I can only bear it. Now this problem has been solved, please see the following method. We have passed into this basic type, then add the value of i directly to the list, in fact, the list cannot store basic types, the object should be stored in the list, where the compiler will be packaged into Integer, then add it to the INT Go to the list. Then we use list.get (0); to retrieve data, and return to the object to unpack the object into int. Well, JDK5.0 brings us more convenient and safe.
// code list 7 public void autoboxingunboxing (INT i) {ArrayList
1.2.3. The range of type parameters in generics may have been discovered in the Testgen
// Code List 8 Class Testgen2
Public void setv (v V) {this.v = v;} public v getv () {return this.v;} public void setk (kk) {this.k = k;} public v getk () {Return THIS Public static void main (string [] args) {testgen2
A
A1
A2
A2_1
A2_2
figure 1
Please see Figure A is the base class, A1, A2 in the topic, A1, A2, respectively, and 2 subclasses are A2_1, A2_2, respectively.
Then we define a restricted generic class mygen
This is a single restriction, you can also limit your number, as follows:
Class C
Let's analyze the following sentence, t extends comparable This is the limit of the upper limit, and Comparable Super t> This is the limit of the lower limit, serializable is the second upper limit. A specified type parameter can have one or more upper limits. Type parameters with multiple restrictions can be used to access each limitation of each limit.
1.2.4. Polymorphism
// Code List 9
Class testgen {
Return New List
}
}