java study notes (Basics) - reflection

A reflective: the self-describing or custom

A class or object may itself be obtained by the reflection object, the object is a java.lang.Class
object (as an image file). Obtaining a class object or process itself is referred to as reflected Class object.
java.lang.Class: class is Mirror the Object Class class mirroring effect may get all the attributes, methods and constructors of the object class and, methods and constructors and property modifiers, modify the value of the private property. Call method and call the constructor, and so on.
Examples of class ---- -----> ----- instantiated class -----> Object
Object abstract ----- ------> ------ class abstract -----> Class
abstract: with the same properties and behavior of the abstract

public final class Class{
      Field[] fileds;
      Method[] methods;
      Constructor[] cons;
}
Reflection API:
java.lang.Class
java.lang.reflect.Field
java.lang.reflect.Method
java.lang.reflect.Array
java.lang.reflect.Constructor

Person {} class
Person create 100 objects, Class type of object number Person of creation?

1, when the class loader created Class of Person type objects.

Step by reflection operation:

1) acquires image class (Class object), three ways
A) the Class.forName (String className)
B) data type .class
C) objects .getClass ()
2) (does not create new objects by way of the mirror through the class object instance )

Object obj = class image objects .newInstance ().

The default class will call the class constructor with no arguments.

3) By way of example of the operation of the object properties, methods, constructors
a) obtaining all of the attribute (name and data type and modifiers).
b) obtaining all of the methods (method name and modifiers, and return values and parameter list)
C) Get all constructors (name and parameter list and modifiers)
D) attribute modification value
e) calling the method: Invoke ()
F) call Object constructor to create the specified

When reflection:

1) at run time before we know the class name
2) at run time before we know the attribute name
3) at run time before we know the method name

reflection:

1) determining a class object
2) can obtain a modifier class, fields, methods, and constructors parent.
3) obtaining constants and methods declared in the interface.
4) create an instance of Class obtained until runtime.
5) before running even if you do not know the name field, access and modify the values of these fields can be when the program is running.
6) before running the method name, even if the object does not know, you can trigger a call to the method when the program is running.
7) before running the size and creates a new array elements are unknown,
modify elements of the array can be when to run.

Use of reflection:

1) modify the value of the private property of
any method 2) call any object
3) does not create new objects by way of

Use reflection Note:

1) reflection allows us to take advantage of this Class object to retrieve and modify private variables and methods, not to get (our original example is to set and get variables by means of a private-public) through a common method, can destroy data encapsulation.
2) reflection by probing at runtime fields and methods, which can help write a good universal program, this capability is particularly useful for systems programming, but it is not suitable for application programming. Moreover, the reflection is fragile - compiler can not help you find a compilation error, any error was detected at runtime and will result in an exception. Lead to unsafe code.

Guess you like

Origin www.cnblogs.com/chlinlearn/p/11236341.html
Recommended