Java generic Quik Start

xiaoxiao2021-03-06  36

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 , which is also variable in the K and V on this. Let's take a look: // code list 1 Import java.util.hashtable; Class testGen0 {public hashtable h = new hashtable (); public void Put (k K , V V) {H.PUT (k, v);} public v get (k) {return h.get (k);} public static void main (string args []) {TestGen0 t = New TestGen0 (); T.PUT ("key", "value"); string s = t.get ("key");

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 . Then we call it, the question will appear, we will pass a list as a method to the method, JVM will give us a warning, saying that this destruction type is safe, because it returns from the list is Object Type Let's take a look at the way below.

// code list 3 void TestGen0Medthod1 (List L) {for (Object O: L) System.out.println (O);} Because of the list not list , not String The relationship with Object, that is, List is not affiliated to list , they are not inheritance, so it is not possible, and the extends here indicate restrictions. Type wilders are very magical, List What can you do for him? How is "?", It seems uncertain, he can't return one? As the type of data, it is ah, will he return a "?" To ask the programmer? JVM will make simple thinking, look at the code, it is more intuitive.

// code list 4 list l1 = new arraylist (); li.add ("string"); list l2 = l1; system.out.println (l1.get (0)); This code is no problem, L1.Get (0) will return an object.

1.1.2. Writing generic classes Note: 1. When defining a generic class, define the form type parameters between "<>", for example: "Class Testgen ", "K" , "V" does not mean the value, but is the type. 2. When instanting generic objects, be sure to specify the value of type parameters (type) after the class name, a total of two writing. For example: Testgen t = new testgen (); 3, the , extend is not inherited, it is a type range limit. 1.2. Pan and data type conversion 1.2.1. Elimination Type Conversion The above example is what everyone sees, the code of data type conversion is not seen. We often have to write the following code, such as:

// 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 h = new hashtable (); h.put ("key ", new integer (123)); int s = h.get (" key "). INTVALUE (); system.out.println (s);}} We use generalized version of HashMap, so you don't have to come Writing type conversion code, the process of type conversion is handed over to the compiler to handle it, it is very convenient, and it is safe. The above is String mapped to String, or you can map the Integer to string, as long as you write a HashTable h = new hashtable (); h.Get (NEWEGER (0)) Returns Value. Sure enough.

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 L = new arraylist (); l.add (i); int a = L.GET (0); System.out.Println "The value of i is" a);

1.2.3. The range of type parameters in generics may have been discovered in the Testgen of Code List 1, where K, V can be any type. Maybe you sometimes want to limit K and V of course, how to do it? Take a look at the following code:

// Code List 8 Class Testgen2 {Private V = Null; Private K K = NULL;

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 t2 = new testgen2 (); t2.setk ("string"); t2.setv ( New Integer (123)); System.out.Println (T2.GETK ()); System.out.Println (T2.Getv ());}} The range of on the upper edge is <= String, V is <= Number, pay attention to "<=", for K can be String, and V is of course Number, or INTEGER, FLOAT, DOUBLE, BYTE, etc. Look at the picture below may be intuitive

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 generic range is the middle map of the middle.

This is a single restriction, you can also limit your number, as follows:

Class C & Serializable>

Let's analyze the following sentence, t extends comparable This is the limit of the upper limit, and Comparable 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 {

Public Static List Make (t first) {

Return New List (first);

}

}

转载请注明原文地址:https://www.9cbs.com/read-61956.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.044, SQL: 9