Generic, that is, “parameterized types”. That is, the type is parameterized from an original concrete type, similar to a variable parameter in a method. In this case, the type is also defined as a parameter, which can be called a type parameter, and then passed in the concrete type (the type argument) when the callback is used.

A generic class

Creating a ClassName is going to be a type and all the T’s in it are going to be strings

If you pass in an int it’s going to be an int



Invoke generic classes

A generic interface

1. The interface to generics is the same as the method usage of generics

Generic method



There’s an auto-unboxing that if I just pass in String directly in print then his A will automatically become A String

Generics restrict types

1. When using generics, you can specify a qualified region for generics

– For example: must count a subclass of x or an implementation class of the x interface

– <T extends class or interface 1 & interface 2>

Wildcard “?” in generics

Which wildcard is used for the type? Instead of method specific type arguments

1. <? Extends parent> specifies the last class of a generic type

demand



Previous use of wildcards

2. <? Super Child> specifies the next generation of a generic type

3. <? > specifies unrestricted generic types

2. Types in generics are specified at use without casting (type safety, compiler checks type)

\

This article is shared from wechat official number – May Heaven have no BUG (MA214617). If there is infringement, please contact [email protected] to delete. This article participates in “OSC source innovation Program”, welcome you to join us and share with us.