Java notes-Generics

Source: Internet
Author: User

1, the generic solution to the security of the element storage, to solve the acquisition of data elements, the need for the type of strong-turn problem.

--the core idea of generics: to limit the content in a collection to a specific data type.

2, the use of generics

1) use in the collection

2) Custom generic class, generic interface, generic method

3) The relationship between generics and inheritance

4) wildcard characters

3, if the generic class, the generic interface does not use generics, the default is the object type.

4. When inheriting a generic class or generic interface, you can indicate the type of the generic type.

5. Definition of generic method:

 public <E> e Gete (e e) {    return  e;}

6. Class A is a subclass of class B, but list<a> is not a list<b> sub-interface.

7. The use of:list<a>, List<b> Are all list<?> sub-interfaces.

--? Extends a: can store a and its subclasses;

--? Super A: Can hold a and its parent class.

--The object is not allowed to be written to a collection class declared as a wildcard, but can be read.

8, the generic note points:

1) The generic type of the class cannot be used in a static method;

2) If the generic class is an interface or abstract class, the object of the generic class cannot be created;

3) cannot use generics in catch

4) derive subclasses from generic classes, and generic types need to be materialized.

Java notes-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.