Simple to use generics

Generics: a definite type of work to be postponed until the time to create an object or method call before going to clear type, parameter types, the type passed as a parameter the same.

format:

<Data Type>

Data type can only be referenced here

benefit:

A: The problem is running ahead of time to compile

B: Avoid cast

C: optimized programming to solve the yellow warning line

Generic class

The generic class defined in the

Format: public class name of the class <generic type 1, ....>

Note: The generic type must be a reference type

Generic method

The generic method defined in

Format: public <generic type> method Name Return Type <generic type>

Generic Interface

The generic definition of the interface

Format: public interface interface name <generic type 1, ........>

When the implementation class that implements the interface

The first case: already know what type of a

The second situation: do not know what type of

Senior generic (wildcard)

Generic wildcard <?>

Any type, if not explicitly, then that Object as well as any of the java class.

? extends E

Defining downwardly, E and its subclasses

? overcome

Upwardly defined, E and its parent

You must be consistent when writing generic

 

Guess you like

Origin blog.csdn.net/qq_40935960/article/details/94958626