One day a Java foundation-generics

Source: Internet
Author: User

This semester of the new class-design mode, by my long-admired teacher to teach, unfortunately thinking too fast, the first section was the teacher picked up the code, from this in the heart of the Shadow, is the Java Foundation owed debt

This semester's new class-Algorithm design and analysis, although the teacher does not love and classmates interactive lectures, but the teacher is very good, but due to the data structure of the lack of class listening to a bit of burning the brain, are the debt

This semester's new class-English speaking, although the foreign teacher coquettish playful loose love freedom, but my generation vocabulary lack, indicating calm said yeah, but heart 10,000 grass mud horse galloping, are English owed debt

1. Generic class

Entity classes (container classes), classes that are frequently reused, and the following is an entity class that does not have a generic type:

1  Public classuser{2     PrivateString username;3     Private intNumber ;4      PublicString GetUserName () {5         returnusername;6     }7      Public voidSetusername (String username) {8          This. Username =username;9     }Ten      Public intGetNumber () { One         returnNumber ; A     } -      Public voidSetnumber (intNumber ) { -          This. Number =Number ; the     } -      PublicString toString () { -         return"User [username=" + Username + ", number=" + number + "]"; -     } +      PublicUser (String username,intNumber ) { -         Super(); +          This. Username =username; A          This. Number =Number ; at     } -      PublicUser () { -         Super(); -     } -}

Attribute number can be used as a student's ID, if it is an int type, 11-digit number is sufficient, but if it is ID, one is the length is not enough, the second is the existence of the character X, so it needs to be redefined, so that the reusability of this entity class is very low.

But if you use generics, that's it:

1  Public classUser<k,v> {2     PrivateK Usrename;3     PrivateV number;4      PublicK Getusrename () {5         returnUsrename;6     }7      Public voidsetusrename (K usrename) {8          This. Usrename =Usrename;9     }Ten      PublicV GetNumber () { One         returnNumber ; A     } -      Public voidSetnumber (V number) { -          This. Number =Number ; the     } -      PublicString toString () { -         return"User [usrename=" + Usrename + ", number=" + number + "]"; -     } +      PublicUser (K usrename, V number) { -         Super(); +          This. Usrename =Usrename; A          This. Number =Number ; at     } -      PublicUser () { -         Super(); -     } -}

The benefits are:

1  Public classTest1 {2      Public Static voidMain (string[] args) {3user<string,integer> U =NewUser<string,integer>();4U.setusrename ("Zhengbin");5U.setnumber (2013734217);6user<string,string> u1 =NewUser<string,string>();7U1.setusrename ("Zhengbin");8U1.setnumber ("4*****19951029****");9 System.out.println (u);Ten System.out.println (U1); One     } A}

Operation Result:

User [Usrename=zhengbin, Number=2013734217]user [Usrename=zhengbin, number=41****19951029****]

Attention:

(1) As a rule, a single uppercase letter, such as E or T, is used to denote a formal generic type

(2)

    

2. Generic interface

3. Generic methods

One day a Java foundation-generics

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.