Since jdk1.5 and later, Java has added generics. The following describes a few demos from the application aspect and briefly describes the usage of generics.
Reference: http://www.cnblogs.com/sunwei2012/archive/2010/10/08/1845938.html
1. Basic Generic usage, wildcard, and restricted Generic usage
/**
* Basic Generic usage, wildcard, and restricted Generic usage
* @ Author
* @ Param <t>
*/
Public class point <t> {// you can write the identifier here. t is short for type.
Private t var; // The type of VaR is specified by T, that is, by external
Public t getvar () {// The type of the returned value is specified externally
Return var;
}
Public void setvar (T var ){
This. Var = var;
}
// Wildcard
Public static void testwildcard (point <?> P) {// can receive any generic object
System. Out. println ("wildcard content:" + P. getvar ());
}
// Restricted Generic
Public static void testlimit (point <? Extends number> P) {// only receives subclasses of number and number.
System. Out. println ("restricted Generic content:" + P. getvar ());
}
// Test
Public static void main (string ARGs []) {
// Test Basic Generic usage
Point <string> P = new point <string> ();
P. setvar ("test ");
System. Out. println (P. getvar (). Length ());
Point <long> plong = new point <long> ();
Plong. setvar (1l );
System. Out. println (plong. getvar ());
// Wildcard
Testwildcard (plong );
Testwildcard (P );
// Restricted Generic
Testlimit (plong );
}
}
/**
* Two generic types
* @ Param <k>
* @ Param <v>
*/
Public class notepad <K, V> {// two generic types are specified here
Private K key; // The type is specified externally
Private V value; // The type is specified externally.
Public K getkey (){
Return key;
}
Public void setkey (K key ){
This. Key = key;
}
Public v getvalue (){
Return value;
}
Public void setvalue (V value ){
This. value = value;
}
// Test
Public static void main (string [] ARGs ){
Notepad <string, integer> notepad = new notepad <string, integer> ();
Notepad. setkey ("Tom ");
Notepad. setvalue (20 );
System. Out. println ("name:" + notepad. getkey () + "Age:" + notepad. getvalue ());
}
}
2. generic interface:
/**
* Generic interface
* @ Param <t>
*/
Public interface info <t> {// define the generic type on the Interface
Public t getvar (); // defines the abstract method. The return value of the abstract method is generic.
}
// Method 1
Public class infoimpl <t> implements info <t> {// defines a subclass of a generic interface
Private t var;
Public infoimpl (T var ){
This. setvar (VAR );
}
@ Override
Public t getvar (){
Return this. var;
}
Public void setvar (T var ){
This. Var = var;
}
}
// Method 2
Public class infoimpl2 implements info <string> {// defines a subclass of a generic interface
Private string var;
Public infoimpl2 (string var ){
This. setvar (VAR );
}
@ Override
Public String getvar (){
Return this. var;
}
Public void setvar (string var ){
This. Var = var;
}
}
// Test
Public class genericinfoimpl {
Public static void main (string [] ARGs ){
// Method 1
Info <string> info = new infoimpl <string> ("Tom ");
System. Out. println ("content:" + info. getvar ());
// Method 2
Info info2 = new infoimpl ("Tom ");
System. Out. println ("content2:" + info2.getvar ());
}
}
3. Generic method:
/**
* Generic Method
*/
Public class genericmethod {
Public <t> T test (t) {// can receive any type of data
Return T;
}
// Test
Public static void main (string [] ARGs ){
Genericmethod T = new genericmethod ();
System. Out. println (T. Test ("Tom "));
System. Out. println (T. Test (200 ));
}
}
4. Return the generic type instance through the generic Method
/**
* Returns a generic instance using the generic method.
* @ Param <t>
*/
Public class returngeneric <t extends number> {
Private t var;
Public t getvar (){
Return var;
}
Public void setvar (T var ){
This. Var = var;
}
@ Override
Public String tostring (){
Return var. tostring ();
}
// Test
Public static void main (string [] ARGs ){
System. Out. println (fun (100 ));
}
// The generic type passed in or returned in the method is determined by the parameter type set when the method is called.
Public static <t extends number> returngeneric <t> fun (t param ){
Returngeneric <t> temp = new returngeneric <t> (); // instantiate info based on the input data type
Temp. setvar (PARAM );
Return temp;
}
}
5. Generic Array
/**
* Generic Array
*/
Public class arraygeneric {
Public static void main (string [] ARGs ){
Integer I [] = fun1 (1, 2, 3, 4, 5, 6, 7, 8 );
Fun2 (I );
}
Public static <t> T [] fun1 (T... Arg) {// receives variable parameters
Return ARG; // returns a Generic Array.
}
// Test
Public static <t> void fun2 (T Param []) {
System. Out. println ("receiving generic arrays ");
For (T: Param ){
System. Out. println (t + ",");
}
}
}
6. nested settings of generics
/**
* Two generic types
* @ Param <k>
* @ Param <v>
*/
Public class notepad <K, V> {// two generic types are specified here
Private K key; // The type is specified externally
Private V value; // The type is specified externally.
Public K getkey (){
Return key;
}
Public void setkey (K key ){
This. Key = key;
}
Public v getvalue (){
Return value;
}
Public void setvalue (V value ){
This. value = value;
}
}
/**
* Generic nesting settings
* @ Author gongpb
* @ Param <S>
*/
Public class nest <S> {
Private s var;
Public nest (s var ){
This. setvar (VAR );
}
Public s getvar (){
Return var;
}
Public void setvar (s var ){
This. Var = var;
}
// Test
Public static void main (string [] ARGs ){
Notepad <string, integer> NP = new notepad <string, integer> ();
NP. setkey ("Tom ");
NP. setvalue (25 );
Nest <notepad <string, integer> nest = new nest <notepad <string, integer> (NP );
System. Out. println ("name:" + nest. getvar (). getkey () + "Age:" + nest. getvar (). getvalue ());
}
}