7.21 Summary--use of reflection

Source: Internet
Author: User

Today, a reflection of the day, in fact, the reflection itself is very simple, but the use of relatively complex;

Let's talk about what reflection is. The power of reflection is that he can invoke any property in the native Java file, either private or public, through a. class file.

Note: A class that reflects calls must have an argument-free constructor

Now let's look at three ways to get a reflection template:

1) class name.

Eg:class Clazz1=reflect01.class;

2) First new object, in use object. GetClass get

eg:reflect01 reflecto1=new Reflect01 ();
Class Clazz2=reflecto1.getclass ();

3) Class.forName (package name + class name)

Eg:class clazz3=class.forname ("Com.cdsxt.test.vo.User"); this throws an exception because the path may be incorrect and requires Trycatch

Reflect a few common APIs:

Getsimplename: Gets the class name;

Getname:1) class GetName method: Gets the package name + class name, which is the full class name

2) GetName method in field class: Gets the name of the property

Getpackage: Gets the package name, which is the entire package path from SRC

GetType methods in the Gettype:field class: Get the type of the property

GetField: Gets the Property object of the passed argument

GetFields: Gets all the property objects in the class

Getdeclaredfield and Getdeclaredfields: Gets the private Property object, as long as there are declared can get the private method, belongs to ....

Newinstance: Get Class object

/*
* Methods for setting property values
* 1) a) Gets the Property object: Clazz.getfield ("Property name")
* b) Get class object: Clazz.newinstance ();
* c) Assignment by the Set method: Gets the Property object. Set (Gets the class object, value);
* d) through the Get method to the value: Gets to the Property object. Get (gets to the class object)
*/

/*
* How to get methods by reflection templates and how to call methods
* 1) Three ways to get the class, class name. Class,class.forname ("package name + Class name"), first new object, in use object. GetClass
* 2) Clazz.getmethods () returns an array of methods, Clazz.getmethod (method name, Parameter list "type. CALSS"), private, plus declared
* 3) GetName () Gets the method name, Getreturntype () Gets the return value type
*/
/*
* Call Method 1 through Method object
* 1) a) Get the method on the template: Getdeclaredmethod ("Method name")
* b) The Invoke method: Method.invoke (obj), no parameter, no argument, there is a parameter corresponding to the method, in addition, static NULL, non-static transfer obj
* C) Finally achieve the purpose of invoking the method
*
*/

Reflection application: Using reflection to encapsulate database additions and deletions, the advantage is that you can query any type of table

When using reflection encapsulation, it also uses the generic parameter, the generic simple point is the indeterminate type, what type of parameter you pass, what type the generic represents.

  

  

7.21 Summary--use of reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.