In Java, no matter how many objects of a class are generated, these objects correspond to the same class object.
The Dynamic Language is:ProgramLanguage that allows you to change the program structure or variable type during runtime
Ruby is a dynamic Language
Java is a static Language
Reflection reflection is a dynamic mechanism in Java
Java. Lang. Reflect
The class is located in Java. Lang.
Field
Method
Constructor
Array
Simple Example:
Obtain the class object corresponding to the class
Class classtype = test. Class;
Create a class instance
Object o = test. newinstance ();
Obtains a specified method object.
Method addmethod = classtype. getmethod ("add", new class [] {Int. Class, Int. Class });
The first parameter is the method name, and the second parameter is the class object for passing the Parameter
Invoke calls the target method
Object result = addmethod. Invoke (O, new object [] {1, 2 });
1. To use reflection, first obtain the class object corresponding to the class to be processed.
2. Three common methods for obtaining class objects corresponding to a class or an object
A) use the static class method forname, class. forname ("Java. Lang. String ");
B) Use the. Class syntax of the class: String. Class;
C) use the getclass () method of the object: String S = "AA"; Class <?> Clazz = S. getclass ();
3. If you want to generate an object using the class constructor without parameters, we have two methods:
A) first obtain the class object, and then generate it directly using the newinstance () method of the Class Object:
Class <?> Classtype = string. Class; object OBJ = classtype. newinstance ();
B) first obtain the class object, then obtain the constructor object used by the object, and then generate it using the newinstance () method of the constructor object;
Class <?> Classtype = Customer. Class;
Constructor cons = classtype. getconstructor (new class [] {});
Object OBJ = cons. newinstance (new object [] {});
4. If you want to generate an object using the constructor with parameters of the class, you can only use the second method.
Class <?> Classtype = Customer. Class;
Constructor cons = classtype. getconstructor (new class [] {sring. Class, Int. Class });
Object OBJ = cons. newinstance (new object [] {"hello", 3 });