Today’s sharing started, please give us more advice ~

The generic definition

Popular, generic is broad types, there is no specific type, needs to be introduced to a certain type definitions, and only to a particular type and limits the scope of use, so to a generic can very good solution to this awkward problem, this paper from the four aspects of the generic type, mastered the four aspects, the use of generics would swim blade enough to spare. In plain English, generics are really a type placeholder. I’m sure you’ve all used generics in your daily work, such as defining a collection object.

Example:

A generic class

A generic class is defined by specifying a generic type for a variable or method terrain parameter in the class.

Example 1:

Define a class, define a type for the variables in the class, we don’t know what type the variables are, so we can use generics, let’s call it T, but of course you can use any letter.

When creating the User1 object, we can pass in the desired type. Such as:

Example 2:

In example 1, all arguments are of the same type, although different arguments can be specified.

When creating the User2 object, we can pass in two different types. Such as:

Generic method

The so-called generic method is the method parameter type is a generic, divided into ordinary generic method and static generic method.

Generic methods

There are two ways to define common generic methods, one that depends on placeholders passed in by the generic class, and the other that are held by the method itself.

Example:

When defining a class, type the method parameters in the class.

Say1 () must be called with the same type as the generic class passed in; say2() is not restricted. Such as:

Static generic methods

Static generic methods do not depend on the placeholder of the generic class and hold a placeholder of their own.

Examples of errors:

Correct examples:

A generic interface

Generic interfaces, like generic classes, specify a placeholder when defining the interface. An implementation class may specify a placeholder type, or it may not specify a placeholder type, which defaults to Object.

Example 1:

If the implementation class also does not specify a type and does not use the default type, then placeholders must be used that are consistent with the interface’s placeholders.

Example 2:

Generic erasure

After Java types are compiled to generate bytecode, generics are removed from the type during the reflection phase. This is called generic erasure, which means that generics in Java exist only during the compilation phase. At run time, the types of generics are erased. The goal is to be compatible with previous versions of the JDK.

Example:

The wildcard

There is inheritance in generics, but it is different from inheritance in Java. Generic relationships in Java classes are not recognized in generics. So you have to pay attention to this one.

Example 1:

In Java terms, user31 should be able to be set, but generics don’t actually support it. To solve this problem, use wildcards to rebind inheritance relationships in Java. Are wildcards commonly used? Student: Is it? Is the parent of a generic.

Example 2:

Although it will compile, it doesn’t really matter, because it’s using? The idea is that we can pass in a subclass of Number. So this is the question of the upper and lower bounds of generics.

Upper boundary (extends)

Student: For the above? < span style = “box-sizing: border-box; color: RGB (50, 50, 50); line-height: 20px; font-size: 14px! Important; white-space: inherit! Important;” That is, with extends, the type value passed in must be a concrete subclass of a class.

Example:

Upper bounds, which are used to read data of type T, are equivalent to the son’s inability to change the parent’s past.

Lower boundary (super)

Upper bounds are easy to understand, lower bounds are easy to understand, as long as the generic is a parent of a concrete class.

Example:

The lower bound, which is generally used when writing data of type T, is the equivalent of a father taking care of his son.

Today’s share has ended, please forgive and give advice!