What is generic? Let's take a look at what Baidu encyclopedia says:
Generic is a new feature introduced by jdk1.5. The nature of generics is parameterized, that is, the operated data type is specified as a parameter. This type of parameter can be used to create classes, interfaces, and methods, which are called generic interfaces, generic methods, and so on.
So why does Java introduce generics?
At first, the advantage of introducing generics in Java is that it is secure and simple, because we know that when using containers, such as List, an Object type is added to List, therefore, it is unknown whether the required types are added during the compilation phase. As follows:
List users = new ArrayList (); User user = new User (); user. setName ("Zhang San"); list. add (user );
If I have not added a User type, the compilation will not be problematic, but the runtime will certainly encounter errors. After the introduction of generic type in jdk1.5, the List writing method has changed, that is, the generic type is added, so that problems can be detected in a timely manner in the compilation phase. As follows:
List
Users = new ArrayList
(); User user = new User (); user. setName ("Zhang San"); list. add (user );
If a User instance is not added to the List, an error is returned.
If the generic model only has such a function, it will be too small to look at sun.
As mentioned at the beginning, generics can be declared to the creation of classes, interfaces, and methods, so we will illustrate the three types respectively (in fact, I prefer to say two types ):
Declared on the class:
The beauty of the last encapsulation: parent and children cannot be enclosed in the parent class. Here we will look at how to mention the parent class:
Parent class: Tree. java
public class Tree
{private String id;private String name;private T parent;private String pid;private List
children;private boolean isleaf;}
Subclass: Module and Organization
public class Organization extends Tree
{private String description;}public class Module extends Tree
{private String url;private String description;private String sn;}
In this way, getParent is a Module instance in the Module instance, and getParent is a Organization in the Organization instance. If the generic type is not used, this can only be declared in the subclass, or declare the Tree in the parent class (this will cause problems ).
In this case, each class uses its own type, so it declares a parent class with a generic type and determines the type of the variable during inheritance.
Declarations on interfaces/methods:
In this way, I personally think that the type is treated as a parameter and I will confirm it during compilation. For example, let's take a look at it:
Interface:
T get (Class
EntityClass, Object pk); Implementation: public
T get (Class
EntityClass, Object pk) {T obj = em. find (entityClass, pk); return obj;} method call: User user User = get (User. class, "001 ");
I do not know whether it is clear. When declaring methods and interfaces, parameters and return values are declared generic. During the call, this parameter type and return value type will be determined, the advantage of this is that method declarations are more universal.
But can we achieve universality by simply relying on this one? The method above is actually the method of calling the EJB EntityManager. If there are other operations instead of calling this method, what should we do?
Here, I just want to give you a bit of reflection, that is, the items under the java. lang. reflect package. Wildcard and reflection are only a little more recently exposed, and they are not very good at use, but they have really felt the convenience they bring to programming. Just like this ~~