Generic programming---Definition of generic classes

Source: Internet
Author: User

Generic programming, programming all understand what meaning, that generics? What is a generic type?

The so-called generic refers to: the amount ... Well, I don't know. For example: There is a sentence in ancient religious Taoism, Daosh One, life two, two three, Sansheng everything. The Tao here corresponds to the generic type in the program design, the generics are very abstract, I can not say why ... (You can leave a message under the article to discuss)

So how do you define a generic class?

The syntax is as follows:

Before you look at the syntax, you need to understand the concept of type variables. The so-called type variable refers to: can replace any type of a variable, when defined, you can use the character (general capitalization, and general use of T and its adjacent characters); Actually, when declaring a variable of a generic class, you need to use a specific class name instead of a type variable.

Class name < type variable ...;: type variable can have multiple, each type variable is separated by a comma ",".

The following is an example of a generic class program with 1 type variables and multiple type variables:

/*** dateandtime:2016-12-14 pm 10:15:03*/ PackageChapter12;/*** Test: definition of a generic class of 1 type variables *@authorAdministrator **/ Public classGenerictypeclass { Public Static voidMain (string[] args) {string[] words= {"Mary", "had", "a", "little", "lamb"}; Pair<String> pair =Arrayarg.minmax (words); System.out.println ("min:" +Pair.getfirst ()); System.out.println ("Max:" +Pair.getsecond ()); }}/*** A GenericType Class *@authorAdministrator * *@param<T>*/classPair<t> {        PrivateT first; PrivateT Second; Pair () { This. First =NULL;  This. Second =NULL; } Pair (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; }    }classArrayarg { Public StaticPair<string>Minmax (string[] t) {if(NULL= = T | | T.length = = 0)return NULL; String min= T[0]; String Max= T[0];  for(String element:t) {if(Element.compareto (min) < 0) min =element; if(Element.compareto (max) > 0) max =element; }        return NewPair<>(min, max); }    }
View Code
/*** dateandtime:2016-12-14 pm 10:36:44*/ PackageChapter12;/*** Definitions of generic classes for multiple types of variables *@authorAdministrator **/ Public classGenericTypeClass2 { Public Static voidMain (string[] args) {pairmultitypeparamters<string, integer> pmtp =Newpairmultitypeparamters<> ("Hello", 0); System.out.println ("T:" +Pmtp.getfirst ()); System.out.println ("U:" +Pmtp.getsecond ()); }}classPairmultitypeparamters<t, u> {    PrivateT first; PrivateU Second; Pairmultitypeparamters () { This. First =NULL;  This. Second =NULL; } pairmultitypeparamters (T First, U second) { This. First =First ;  This. Second =second; }         PublicT GetFirst () {return  This. First; }         Public voidSetfirst (T first) { This. First =First ; }         PublicU Getsecond () {return  This. Second; }         Public voidSetsecond (U second) { This. Second =second; }    }
View Code

Generic programming---Definition of generic classes

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.