Home > Java > javaTutorial > body text

Generic methods in Java

高洛峰
Release: 2016-12-19 15:14:15
Original
1874 people have browsed it

I won’t go into detail here about what generics mean, 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 t = new Test();, indicating that the type of the generic T is Object.

But generic methods in Java are more complicated.

                                                                                                                                                                             Â           Â               Â                                                  Specific type of generic types when instantiating the class.语 Define the generic format of generic method and grammar as follows:

Generic methods in Java Call the generic method and grammar format as follows:

Generic methods in Java To explain, when defining the generic method, you must add a & lt; t & gt;, come to come before the return value. Declare this to be a generic method, holding a generic T, and then use the generic T as the return value of the method.

   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 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.

      The parameter required by the generic method is 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, the generic method is not just one parameter class & lt; t & gt;, you can add other parameters 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.


For more articles related to generic methods in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!