Generics in Java

Source: Internet
Author: User

1. The concept of generics

  The type of generalization that we call generics in Java, such as the generic type when you define the iterator method <E> refers to an actual declaration that can replace the letters in <> with a specific reference type, such as

Arraylist<> ();

The statement implements the declaration of a ArrayList container that points to a string object.

2. The role of generics

In order to avoid the hassle of type conversion when using containers, it is possible to define a container with generics and make reference types to ensure type safety, while dynamic binding does not require explicit type conversions.

3. Generics customization

In custom generics, the <> of generics is placed after the type, and parentheses can be represented by a single letter (E:element, T:type, K:key, V:value)

1  Public classStudent <T>{2     PrivateT grade;3     4      PublicStudent (T grade) {5          This. grade =grade;6     }7      PublicStudent () {8     }9     Ten      PublicT Getgrade () { One         returngrade; A     } -  -      Public voidSetgrade (T grade) { the          This. grade =grade; -     } -}

When defining and using generics, it is important to note that ① generics cannot be applied on a static property ② using generics, the implemented type cannot be a base type and must be a reference type. Similar generics can also be used to define interfaces and methods.

4. Inheritance of generics

The inheritance of generics satisfies: ① If the parent generic is preserved, the subclass is a generic subclass ② if the parent generic is not preserved, the subclass is implemented as needed

1 classFather<t1,t2>{2     3 }4 //the parent generic is all reserved, subclasses need to be preserved all, subclass generic number >= parent class5 classC1<t1,t2,a,b>extendsFather<t1,t2>{6 }7 //the parent generic part is reserved, and the subclass retains the existing generics in the parent class, and the subclass can add other types as needed8 classC2<a,t2,b>extendsFather<integer,t2>{9 }Ten //The parent generic is a concrete type, and subclasses can use generics as they want (they can not be used) One classC3<a>extendsFather<integer,string>{ A } - //The parent class is a non-generic class, and subclasses can or may not be generic classes - classC4<a,b>extendsfather{ the}

5. Generic Erase

Generic erasure refers to the use/inheritance/implementation when no type is specified, the actual type of the default generic is similar to object, but not equivalent to object such as:

1     New ArrayList ();    // similar to the next line 2 //     arraylist<object> ArrayList = new arraylist<> ();

6. Generic wildcard characters and upper and lower bounds

When more than one type can be implemented in <T>, we can use <?> to represent all subclasses of object or object. In addition, through the adoption of extends and super two

keyword, you can specify the type within a range. Such as

1      Public Static voidMain (string[] args) {2         3list<?extendsFruit> List1 =NewArraylist<apple>();4 test (list1);5list<?extendsApple> List2 =NewArraylist<apple>();6 test (LIST2);7list<?extendsApple> List3 =NewArraylist<fujiapple>();8 test (LIST3);9List<?> List4 =NewArraylist<apple>();Ten //test (LIST4); //list<?> list4 equivalent to list<? extends object> List4 so One                             //A formal parameter of extends fruit> type can only receive a Fruit class or its subclasses as a parameter when list< A     } -  -      Public Static voidTest (LIST&LT;?extendsFruit>list) { the //List.add (New Apple ()); //This cannot be added because the list may be a container for Apple's subclasses, and the container of the subclass cannot save a reference to the parent class -List.add (NULL); -}

In the example above, the subclass of Apple,banana class fruit is met, and Fujiapple is the subclass of Apple. When the parameter of the test method is list<? Extends a container of type fruit>, LIST4 cannot be passed in as an argument

Because object is the parent class of the fruit class.

1      Public Static voidMain (string[] args) {2list<?SuperApple> LIST5 =NewArraylist<apple>();3 test1 (LIST5);4list<?SuperFujiapple> List6 =NewArraylist<apple>();5 //test1 (LIST6); //list< The Super fujiapple> type has a range of more than list<? Super Apple> cannot call6list<?SuperApple> list7 =NewArraylist<fruit>();7 test1 (LIST7);8list<?SuperApple> List8 =NewArraylist<object>();9 test1 (LIST8); Ten     } One  A      Public Static voidTest1 (list<?SuperApple>list) { -List.add (Newfujiapple ()); -List.add (NewApple ()); the //List.add (New Fruit ()); //Here the fruit type is Apple's parent class, and the container list can only add objects of the Apple class to ensure security -List.add (NULL); -}

A similar <? The type of the Super Apple> container cannot exceed the Apple type (not the parent of the Apple type), and the range of the types passed into the Test1 as arguments cannot be greater than < The range of Super apple>.

List6 the compiler has an error because the range of the generic type exceeds the Test1 parameter range.

Generics in Java

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.