Learning GoalsMastering the use of generic interfacesMastering the definition and use of generic methodsMastering the use of generic arraysMastering nested settings for generics
all of the previous operations are directly using generic operations in the class, and for Java, generics can be defined and used directly in the interface.
Defining generic Interfacesafter JDK1.5, not only can you declare a generic class, you can declare a generic interface, declare a generic interface that is similar to the syntax for declaring a generic class, and add <t> to the interface name, as shown in the following format:[access rights] Interface interface name < generic identity >{}
Interface info<t>{//defines the generic public T GetVar () on the interface;//define abstract methods, the return value of the abstract method is the generic type}
If a subclass now implements this interface but does not implement it correctly, a warning message will appear at compile time.
Interface info<t>{public T GetVar ();} Class Infoimpl implements info{public String GetVar () { return null;} }
the above operation is not a subclass of the best operation to implement generics, it is best to specify a specific generic type when implemented.
two ways to implement generic interfacesdefine subclasses: The generic type is also declared on the definition of the child class.
Interface info<t>{//defines the generic public T GetVar () on the interface,//defines the abstract method, the return value of the abstract method is the generic type}class infoimpl<t> implements info& Lt t>{//defines the subclass of the generic interface as Private T var;//define Property public Infoimpl (T var) {//Set property content This.setvar (Var) by constructing method;} public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;}}; public class Genericsdemo24{public static void Main (String arsg[]) {info<string> i = null;//Declaration interface Object i = new INFOIMPL&L T String> ("刘勋");//Instantiate Object SYSTEM.OUT.PRINTLN by subclass ("content:" + I.getvar ());}};
if the subclass that implements the interface now does not want to use the generic declaration, then directly specify the specific operation type directly when implementing the interface.
Interface info<t>{//defines the generic public T GetVar () on the interface,//defines the abstract method, the return value of the abstract method is the generic type}class Infoimpl implements Info<string >{//defines a subclass of a generic interface, private String var;//define Property public Infoimpl (String var) {//Set property content This.setvar (Var) by constructing method;} public void SetVar (String var) {this.var = var;} Public String GetVar () {return this.var;}}; public class Genericsdemo25{public static void Main (String arsg[]) {Info i = null;//declares the interface object i = new Infoimpl ("Li Xinghua");//Subclass Instantiate object System.out.println ("content:" + I.getvar ());}};
for the latter, often used. Generic Methodsall of the preceding generics can define a method in addition to specifying the type for the property in the class, and whether the generic class is in the same class as the generic is not in any way related. Defining generic methodsa generic method can define a generic parameter, at which point the type of the parameter is the type of the incoming data, and the generic method is defined using the following format. a simple definition of a generic method:[access rights]< generic label > Generic label Method name ([generic designation Parameter name])The program examples are as follows:
Class Demo{public <T> t Fun (T T) {//can receive any type of data return T;//return parameters directly}};p ublic class genericsdemo26{public static void Main (string args[]) {Demo d = new demo ();//Instantiate Demo object String str = D.fun ("刘勋");//pass string int i = D.fun (24);//pass number, auto boxing Syst Em.out.println (str);//output content System.out.println (i);//output content}};
returning an instance of a generic class through a generic methodas you can see in the previous code, you can pass arbitrary data types whenever you define a generic operation in a method. examples of the program are:
Class Info<t extends number>{//specifies the upper limit, only the number type private t var;//This type is externally determined by public T GetVar () {return this.var;} public void SetVar (T var) {this.var = var;} Public String ToString () {//overwrite the toString () method in the object class return This.var.toString ();}}; public class Genericsdemo27{public static void Main (String args[]) {info<integer> i = Fun (20); System.out.println (I.getvar ());} public static <t extends number> info<t> fun (T param) {info<t> temp = new info<t> ();//Based on the data type instance passed in Infotemp.setvar (param);//Set the passed content to the Var attribute of the Info object return temp;//Return instantiated object}};
types that use generic uniform incoming parametersIf in some operations, the generic type that you want to pass is a consistent type. examples are as follows:
Class info<t>{//Specifies the upper limit, which can only be the number type private t var;//This type is externally determined by public T GetVar () {return this.var;} public void SetVar (T var) {this.var = var;} Public String ToString () {//overwrite the toString () method in the object class return This.var.toString ();}}; public class Genericsdemo28{public static void Main (String args[]) {info<string> i1 = new info<string> (); Info <String> i2 = new info<string> () I1.setvar ("HELLO");//set Content I2.setvar ("Liuxun");//set content Add (I1,I2);} public static <T> void Add (info<t> i1,info<t> i2) {System.out.println (I1.getvar () + "" + I2.getvar ());} };
However, if the two generic types passed to the add order are not uniform, an error occurs.
Class info<t>{//Specifies the upper limit, which can only be the number type private t var;//This type is externally determined by public T GetVar () {return this.var;} public void SetVar (T var) {this.var = var;} Public String ToString () {//overwrite the toString () method in the object class return This.var.toString ();}}; public class Genericsdemo29{public static void Main (String args[]) {info<integer> i1 = new info<integer> (); fo<string> i2 = new info<string> () I1.setvar (20);//set Content I2.setvar ("Liuxun");//set content Add (I1,I2); X: Error occurs here because of type inconsistency}public static <T> void Add (info<t> i1,info<t> i2) {System.out.println (I1.getvar () + "" + I2.getvar ());};
Generic Arraywhen you use a generic method, you can also pass or return a generic array. The procedure is as follows:
public class Genericsdemo30{public static void Main (String args[]) {Integer i[] = fun1 (1,2,3,4,5,6);//return generic array fun2 (i);} public static <T> t[] Fun1 (t...arg) {//Receive variable parameter return arg;//return generic array}public static <T> void Fun2 (T param[]) {//output Out System.out.print ("Receive generic array:"); for (t T:param) {System.out.print (t + ",");}}};
Attention:... Represents a mutable parameter that can be passed as many arguments as an array can handle.nested settings for genericsall generic operations mentioned previously are done directly by instantiating the class, and, of course, the nested settings are also visible at the time of Setup. The program examples are as follows:
class Info <t,v>{//receives two generic type private T var;p rivate V value;p ublic Info (T var,v value) {This.setvar (Var); This.setvalue (value); }public void SetVar (T var) {this.var = var;} public void SetValue (V value) {this.value = value;} Public T GetVar () {return this.var;} Public V GetValue () {return this.value;}}; Class Demo<s>{private s Info;p ublic Demo (s info) {This.setinfo (info);} public void SetInfo (S info) {this.info = info;} Public S GetInfo () {return this.info;}}; public class Genericsdemo31{public static void Main (String args[]) {demo<info<string,integer>> d = null;//Will I NFO as the generic type of the demo info<string,integer> i = null,//INFO specifies two generic type I = new info<string,integer> ("刘勋", 20); Instantiate the Info object d = new demo<info<string,integer>> (i);//Set the object of the info class in the Demo class System.out.println ("content one:" + D.getinfo (). GetVar ()); SYSTEM.OUT.PRINTLN ("Content two:" + D.getinfo (). GetValue ());}};
Summary:1, generics can be defined on the interface, and how it is implemented. 2, generics in the use of the time can be nested operations, as long as according to its operation syntax can be. 3, generic methods on the use of generic tags need to be declared, you can also specify the upper and lower bounds of their operations.
Other applications for Java generics-generic interfaces, generic methods, generic arrays, and nested settings for generics