Home >Java >javaTutorial >Detailed explanation and simple examples of generic methods in Java
Detailed explanation and simple examples of generic methods in Java
高洛峰Original
2017-01-11 14:32:111432browse
java generic method:
I won’t go into detail here about what generics means, and the definition of generic classes in Java is relatively simple, for example: public class Test{}. This defines a generic class Test. When instantiating the class, the specific type of the generic T must be specified. For example: Test
But the generic methods in Java are more complicated.
genetic class, which indicates the specific type of generic type when instantiated; the generic method is to indicate the specific type of generic type when calling the method.
The syntax format for defining a generic method is as follows:
## The syntax format for calling a generic method is as follows:
Explain that when defining a generic method, you must add a in front of the return value to declare that this is a generic method and hold a generic T. Then you can use the generic T as the return value of the method. value. The function of Class is to indicate the specific type of the generic, and the variable c of the Class type can be used to create objects of the generic class. Why should we use variable c to create objects? Since it is a generic method, it means that we do not know what the specific type is, nor what the construction method is, so there is no way to create a new object, but we can use the newInstance method of the variable c to create the object, that is, using reflection to create it. object. but the parameters required by the generic method are of type Class, and the return value of the Class.forName() method is also Class, so Class.forName() can be used as a parameter. Among them, the type of the parameters in the forName() method is the type of the returned Class. In this example, the forName() method is passed in the full path of the User class, so an object of type Class is returned. Therefore, when the generic method is called, the type of variable c is Class, so The generic T in the generic method is specified as User, so the type of variable obj is User. Of course, a generic method can not only have one parameter Class, but other parameters can be added as needed. Why use generic methods? Because a generic class needs to specify the type when it is instantiated, if you want to change to a different type, you have to create it again, which may not be flexible enough; while a generic method can specify the type when it is called, which is more flexible. Thank you for reading, I hope it can help you, thank you for your support of this site! For more detailed explanations and simple examples of generic methods in Java, please pay attention to the PHP Chinese website!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn