Java Generic Learning Notes-(ii) Generic classes

Source: Internet
Author: User

1. Let's start by writing a class box with no generic type:

1  Public classBox {2 3     PrivateObject obj;4     5      PublicBox () {}6 7      PublicObject getobj () {8         returnobj;9     }Ten  One      Public voidsetobj (Object obj) { A          This. obj =obj; -     } -  the      PublicBox (Object obj) { -         Super(); -          This. obj =obj; -     } +  -}

At this point, we can deposit any type of data, anyway, they are the subclass of object, the deposit will automatically type promotion. There is no check method.

Let's change it to a class with a generic type:

1  Public classBox<t> {2 3     PrivateT obj;4     5      PublicBox () {}6 7      PublicT getobj () {8         returnobj;9     }Ten  One      Public voidsetobj (T obj) { A          This. obj =obj; -     } -  the      PublicBox (T obj) { -         Super(); -          This. obj =obj; -     } +  -}

It's that simple, just add <T> after the class name and use T in the class to represent the data type specified by the generic.

2. here by the way, the naming conventions for type parameters:
Theoretically you can make any name you want, but preferably a single capital letter, here are some conventions:

    • E-element (used in collections, because elements are stored in the collection)
    • T-type (Java Class)
    • K-key (Key)
    • V-value (value)
    • N-number (numeric type)
    • S, U, v-2nd, 3rd, 4th types

3. when using this class, we can write:

1 New Box<>(); 2 b.setobj ("AAA"); 3 //  //  Compile error 4 String s = b.getobj ();

4. use multiple type parameters:

As we should have noticed when using map, Map uses generics to set two types of parameters, such as hashmap<string, Integer>. We can also define multiple types of parameters when defining our own generic classes, such as:

1  Public classBox<t1, T2, t3> {2 3     PrivateT1 T1;4     PrivateT2 T2;5     PrivateT3 T3;6     7      PublicBox () {}8     9      PublicBox (T1 t1, T2 T2, T3 T3) {Ten         Super(); One          This. T1 =T1; A          This. T2 =T2; -          This. T3 =T3; -     } the  -      PublicT1 getT1 () { -         returnT1; -     } +  -      Public voidsetT1 (T1 t1) { +          This. T1 =T1; A     } at  -      PublicT2 getT2 () { -         returnT2; -     } -  -      Public voidsetT2 (T2 T2) { in          This. T2 =T2; -     } to  +      PublicT3 getT3 () { -         returnT3; the     } *  $      Public voidsetT3 (T3 T3) {Panax Notoginseng          This. T3 =T3; -     } the  +}

Use:

1 New Box<> ("AAA", ten, 3.14); 2 System.out.println (B.GETT1 ()); 3 System.out.println (B.gett2 ()); 4 System.out.println (B.GETT3 ());

Java Generic Learning Notes-(ii) 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.