This article is based on the 12th chapter of the Java Core Reel, and the more detailed information is in the Java core Reel.
1. Generic types can only be reference types and cannot use basic data types.
2. Type variable meaning
E: set K: keyword V: value T: any type
3. Generic class
3.1 Precautions
Type variable declaration is placed after the class name
3.2 Example
Packagecom. Bluestarwei.generic; Public classPair<t> { PrivateT first; PrivateT Second; PublicPair (t first, T second) { This. First =First ; This. Second =second; } PublicT GetFirst () {returnFirst ; } Public voidSetfirst (T first) { This. First =First ; } PublicT Getsecond () {returnsecond; } Public voidSetsecond (T second) { This. Second =second; } }
4. Generic Methods
4.1 Precautions
The type variable is placed after the modifier, and the return type precedes
4.2 Example
Packagecom. Bluestarwei.generic; Public classgenericmethoddemo{ Public Static voidMain (string[] args) {String s= Getmiddle ("Hello", "World", "Happy"); System.out.println (s);// World } //T ... Indicates that you can enter any number of arguments Public Static<T>t Getmiddle (t ... a) {returnA[a.length/2]; }}
5. Restriction of type variable <t extends boundingtype>
5.1 t should be a subtype of the binding type. The t and binding types can be classes, or they can be interfaces.
5.2 A type variable can have multiple qualifications , such as: T extends comparable & Serializable
5.3 Example
Packagecom. Bluestarwei.generic; Public classBoundingtypedemo { Public Static voidMain (string[] args) {integer[] a=Newinteger[]{1,2,3,4}; intMax =Getmax (a); SYSTEM.OUT.PRINTLN (max); } Public Static<textendsComparable<t>>T Getmax (t[] a) {if(A = =NULL|| A.length = = 0)return NULL; T Max= A[0]; for(inti = 0; i < a.length; i++) { if(Max.compareto (A[i]) < 0) {Max=A[i]; } } returnMax; } }
6. Run-time type checking applies only to the original type
6.1 Check Condition
if instanceof // Compile Error if instanceof // Compile Error if instanceof // true
6.2 GetClass Returns the original type
New Pair<string> ("Hello", "World"); Pairnew pair<integer>; if (A.getclass (). Equals (B.getclass ())) { System.out.println ("true"); // class Com.ebao.gs.pol.nb.test.Pair System.out.println (A.getclass ());} Else { System.out.println ("false");}
7. Java does not support arrays of generic types
7.1 Example
New // Compile Error
7.2 Solution : Put parameterized objects into ArrayList
New Arraylist<pair<string>> ();
8. Qualification of wildcard characters
8.1 Sub-type qualification
8.1.1 pair<? Extends employee> cannot call the Setfirst method, but the GetFirst method can be called
extends employee GetFirst ()voidextends employee)
when calling the Setfirst method, the compiler knows only the subtype of the Employee that is needed, but does not know what the specific type is. It refuses to pass any particular type (? cannot be used to match). when calling the GetFirst method, simply assigning the return value of GetFirst to a reference to an employee is perfectly legal.
8.1.2 Example
Public Static voidMinmaxbonus (manager[] Manager, pair<?extendsManager>result) {if(Manager.length = = 0 | | manager = =NULL)return; Manager Minmanager= Manager[0]; Manager Maxmanager= Manager[0]; for(inti = 0; i < manager.length; i++) { if(Manager[i].getbonus () <Minmanager.getbonus ()) Minmanager=Manager[i]; if(Manager[i].getbonus () >Maxmanager.getbonus ()) Maxmanager=manager[i];} Result.setfirst (Minmanager);//Compile ErrorResult.setsecond (Maxmanager);//Compile ErrorManager m =(Manager) Result.getfirst (); System.out.println (M.getbonus ());
8.2 Super-type qualification
8.2.1 pair< Super manager> can call the Setfirst method, but calling the GetFirst method returns the object (can be type-strong)
void Super Manager) Super Manager GetFirst ()
The compiler does not know the exact type of the Setfirst method, but it can be called with any Manager object (or subtype). However, if you call the GetFirst method, the returned object type is not guaranteed. It can only be assigned to an Object (but can be cast in type).
8.2.2 Example
Public Static voidMinmaxbonus (manager[] Manager, pair<?SuperManager>result) {if(Manager.length = = 0 | | manager = =NULL)return; Manager Minmanager= Manager[0]; Manager Maxmanager= Manager[0]; for(inti = 0; i < manager.length; i++) {if(Manager[i].getbonus () <Minmanager.getbonus ()) Minmanager=Manager[i]; if(Manager[i].getbonus () >Maxmanager.getbonus ()) Maxmanager=Manager[i]; } result.setfirst (Minmanager); Result.setsecond (Maxmanager); Manager m=(Manager) Result.getfirst (); System.out.println (M.getbonus ()); }
8.3 Summary
In general: Wildcard characters with a super-type qualification can be written to a generic object, and wildcard characters with sub-type qualification can be read from generic objects.
8.4 Schedule (Manager Class)
Packagecom.ebao.gs.pol.nb.test; Public classManager {PrivateLong ID; PrivateString name; PrivateDouble Bonus; PublicManager (Long ID, String name, Double bonus) {Super(); This. ID =ID; This. Name =name; This. Bonus =bonus; } PublicLong getId () {returnID; } Public voidsetId (Long id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicDouble Getbonus () {returnbonus; } Public voidSetbonus (Double bonus) { This. Bonus =bonus; } }
9. The meaning of generics
Reducing the use of forced type conversions
For more information, please follow:http://www.cnblogs.com/BlueStarWei/
Generic programming of Java core reels