Click to enter _ more _java thousand ask
1. How to use the generic method
Learn what generics look for: what generics are
The following are the rules that define a generic method:
- When declaring a generic method, before returning a type, you need a part of the generic type delimited by angle brackets (< >).
2. A generic type, also known as a type parameter, is an identifier that specifies the name of a generic type.
- Type parameters can be used to declare return types and to act as placeholders for passing to generic methods.
- The body of a generic method is the same as other methods.
Example:
Public classgenericmethodtest{//generic method PrintArray Public Static< E >voidPrintArray (e[] inputarray) {//Display array elements for(E Element:inputarray) {System. out. printf ('%s ', Element); } System. out. println (); } Public Static voidMain (String args[]) {//Create arrays of Integer, Double and CharacterInteger[] Intarray = {1,2,3,4,5}; Double[] Doublearray = {1.1,2.2,3.3,4.4}; Character[] Chararray = {' H ',' E ',' L ',' L ',' O '}; System. out. println ("Array Integerarray contains:"); PrintArray (Intarray);//Pass an Integer arraySystem. out. println ("Array Doublearray contains:"); PrintArray (Doublearray);//Pass a Double arraySystem. out. println ("Array Characterarray contains:"); PrintArray (Chararray);//Pass a Character array}}
This will produce the following results:
Array Integerarray contains:
1 2 3 4 5 6
Array Doublearray contains:
1.1 2.2 3.3 4.4
Array Characterarray contains:
H E l l O
Generic types can also be restricted by using the extends keyword to restrict the parent class of a generic type.
Example:
Public classmaximumtest{//determines the largest of three comparable objects Public Static<t extends comparable<t>> t maximum (t X, t y, t z) {T max = x;//Assume X is initially the largest if(Y.compareto (max) >0) {max = y;//y is the largest so far}if(Z.compareto (max) >0) {max = Z;//z is the largest now}returnMax//Returns the largest object} Public Static voidMain (String args[]) {System. out. printf ("Max of%d,%d and%d is%d",3,4,5, Maximum (3,4,5) ); System. out. printf ("Maxm of%.1f,%.1f and%.1f is%.1f",6.6,8.8,7.7, Maximum (6.6,8.8,7.7) ); System. out. printf ("Max of%s,%s and%s is%s","Pear","Apple","Orange", Maximum ("Pear","Apple","Orange") ); }}
This will produce the following results:
Maximum of 3, 4 and 5 is 5
Maximum of 6.6, 8.8 and 7.7 is 8.8
Maximum of pear, apple and orange is pear
2. How to use generic classes/interfaces
The declaration of a generic class/interface is similar to a non-generic class, except that a generic type is added after the class name.
The type parameter portion of a generic class can be separated by commas of one or more generic types, as compared to generic methods.
Example:
Public classbox<t> {PrivateT T; Public voidAdd (T t) { This. T = t; } PublicT get () {returnT } Public Static voidMain (string[] args) {box<integer> Integerbox =NewBox<integer> (); Box<string> Stringbox =NewBox<string> (); Integerbox.add (NewInteger (Ten)); Stringbox.add (NewString ("Hello World")); System. out. printf ("Integer Value:%d", Integerbox.get ()); System. out. printf ("String Value:%s", Stringbox.get ()); }}
This will produce the following results:
Integer value:10
String Value:hello World
Java Technology _java thousand questions (0041) _ How generics are used