[Generic] _ other generic application notes

Source: Internet
Author: User

[Generic] _ other generic application notes
Objectives of this chapter:
Understanding the use of generic Interfaces
Understanding the definition and use of generic methods
Understanding the use of generic Arrays
Understanding the nested settings of generics

3.1 generic interface

Definition Format:
[Access permission] interface name <generic ID> {}

3.1.2. Two Methods for implementing generic Interfaces

1. Define subclass. The generic type is also declared in the subclass definition.

Interface info <t> {// define the generic public t getvar () on the interface; // define the abstract method, the Return Value of the abstract method is the generic type} class infoimpl <t> implements info <t> {// defines the subclass private t VaR of the generic interface; // define the attribute public infoimpl (T var) {// set the attribute content through the constructor this. setvar (VAR);} 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; // declare the interface object I = new infoimpl <string> ("Li Xinghua"); // instantiate the object system by subclass. out. println ("content:" + I. getvar ());}};

2. Specify the operation type directly.

Interface info <t> {// define the generic public t getvar () on the interface; // define the abstract method, the Return Value of the abstract method is generic type} class infoimpl implements info <string> {// defines the subclass private string VaR of the generic interface; // defines the attribute public infoimpl (string var) {// set the attribute content through the constructor this. setvar (VAR);} 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; // declare the interface object I = new infoimpl ("Li Xinghua "); // instantiate the object system by subclass. out. println ("content:" + I. getvar ());}};

3.2 generic method (difficult to understand)

Definition Format of generic methods:
[Access permission] <generic identifier> generic identifier method name ([generic identifier parameter name])

Class demo {Public <t> T fun (t) {// can receive any type of data return t; // directly return the parameter }}; public class genericsdemo26 {public static void main (string ARGs []) {demo d = new demo (); // instantiate the demo object string STR = D. fun ("Li Xinghua"); // pass the string int I = D. fun (30); // pass a number to automatically pack the system. out. println (STR); // output content system. out. println (I); // output content }};

3.2.2 return the instance of the generic class through the generic method (more difficult to understand, let's take a look)

Class info <t extends number> {// specifies the upper limit, which can only be a digital private t var; // This type is determined by the external public t getvar () {return this. vaR;} public void setvar (T var) {This. var = var;} Public String tostring () {// overwrite the tostring () method return this in the object class. var. tostring () ;}}; public class genericsdemo27 {public static void main (string ARGs []) {info <integer> I = fun (30); system. out. println (I. getvar ();} public static <t extends Number> info <t> fun (t param) {// <t extends number> a wildcard ID is created here, which has no practical significance! // Info <t> or t Param only treats the identifier as the operation info <t> temp = new info <t> () of an object (); // instantiate info temp based on the input data type. setvar (PARAM); // set the passed content to return temp in the VaR attribute of the info object; // return the instantiated object }};

3.2.3 unified input parameter types using generics

In some operations, the generic types to be passed are consistent.

Class info <t> {// specifies the upper limit, which can only be a digital private t var; // This type is determined by the external public t getvar () {return this. vaR;} public void setvar (T var) {This. var = var;} Public String tostring () {// overwrite the tostring () method return this in the object class. 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"); // sets the content i2.setvar ("Li Xinghua"); // sets the content add (I1, i2);} public static <t> void add (info <t> I1, info <t> I2) {system. out. println (i1.getvar () + "" + i2.getvar ());}};

If the passed content type is inconsistent, an error is returned!

Class info <t> {// specifies the upper limit, which can only be a digital private t var; // This type is determined by the external public t getvar () {return this. vaR;} public void setvar (T var) {This. var = var;} Public String tostring () {// overwrite the tostring () method return this in the object class. var. tostring () ;}}; public class genericsdemo29 {public static void main (string ARGs []) {info <integer> I1 = new info <integer> (); info <string> I2 = new info <string> (); i1.setvar (30); // sets the content i2.setvar ("Li Xinghua"); // sets the content add (I1, i2);} public static <t> void add (info <t> I1, info <t> I2) {system. out. println (i1.getvar () + "" + i2.getvar ());}};

3.3. Generic Array

When using the generic method, you can also pass or return a Generic Array.

Public class genericsdemo30 {public static void main (string ARGs []) {integer I [] = fun1 (1, 2, 3, 4, 5, 6); // returns a Generic Array (important concept) 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 system. out. print ("receiving Generic Array:"); For (T: Param) {system. out. print (t + ",");}}};

3.4 generic nested settings

All the generic operations described above are completed directly by instantiating the class. Of course, nested settings are also displayed during the settings.

Class info <t, V> {// receives two generic types: Private t var; private V value; public 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; Public 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; // use info as the demo's generic type info <string, integer> I = NULL; // info to specify two generic types I = new info <string, integer> ("Li Xinghua", 30); // instantiate the Info Object D = new demo <info <string, integer> (I ); // set the object of the info class in the demo class (too good x) system. out. println ("content 1:" + D. getinfo (). getvar (); system. out. println ("content 2:" + D. getinfo (). getvalue ());}};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.