The main reason for this demo before using the reflection mechanism to manipulate properties is because in the. class file bytecode, the method is in front of the property.
1, obtain a method in a class
Let's look at the method and run the results. Get all the methods using the class Getmethos () method.
Classes to get:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
package
com.aaron.reflect;
public
class
Heros {
private
String name;
//名字
private
String type;
//类型
private
int
camp;
//0,近卫;1,天灾
public Heros(){}
public
Heros(String name, String type,
int
camp) {
super
();
this
.name = name;
this
.type = type;
this
.camp = camp;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getType() {
return type;
}
public
void
setType(String type) {
this
.type = type;
}
public
int getCamp() {
return
camp;
}
public
void
setCamp(
int
camp) {
this
.camp = camp;
}
@Override
public
String toString() {
return
"Heros [\n name="
+ name +
", \n type="
+ type +
", \n camp="
+ camp +
"\n]"
;
}
}
|
The Hero class contains three properties, and corresponding getter and setter methods. There's also a ToString method. This is a very common pojo.
Test class:
12345678910111213 |
package
com.aaron.reflect;
import
java.lang.reflect.Method; public
class
Demo5 {
public
static
void
main(String[] args) {
Class<?> herosClass = Heros.
class
;
Method[] methods = herosClass.getMethods();
for
(Method method : methods) {
System.out.println(method);
}
}
}
|
Theoretically, we'll get all the getter and setter, and then the ToString method
Operation Result:
123456789101112131415 |
public
void
com.aaron.reflect.Heros.setType(java.lang.String)
public
int
com.aaron.reflect.Heros.getCamp()
public
void
com.aaron.reflect.Heros.setCamp(
int
)
public
java.lang.String com.aaron.reflect.Heros.toString()
public java.lang.String com.aaron.reflect.Heros.getName()
public
void
com.aaron.reflect.Heros.setName(java.lang.String)
public
java.lang.String com.aaron.reflect.Heros.getType()
public
final
void
java.lang.Object.wait(
long
,
int
)
throws
java.lang.InterruptedException
public
final
native
void
java.lang.Object.wait(
long
)
throws
java.lang.InterruptedException
public
final void
java.lang.Object.wait()
throws
java.lang.InterruptedException
public
boolean
java.lang.Object.equals(java.lang.Object)
public
native
int
java.lang.Object.hashCode()
public
final native
java.lang.Class java.lang.Object.getClass()
public
final
native
void
java.lang.Object.notify()
public
final
native
void
java.lang.Object.notifyAll()
|
However, it is a list of such a large pile.
Take a closer look at the return method and return the methods owned by the object class. Because our Pojo class inherits the Java.lang.Object class by default. , such as GetClass (), Equals ().
Using GetMethods (), this method returns an array containing some method objects that have been introduced to represent the object of a method, which is the result of abstracting the methods in the class.
These objects reflect the class or interface represented by the class object, including, of course, those classes or interfaces declared by the class or interface and inherited from the superclass and the hyper-interface. An example to explain:
For example, using GetMethods () to obtain an integer class method, the parent class of Integer, all methods of number, and the method of the parent object of number are obtained.
This returns an array that includes all ( public ) member methods inherited from the object class. The elements in the returned array are not sorted. If this class object represents a class or interface that does not have a public member method, or represents a primitive type or represents void, an array of length 0 is returned.
2, calling methods in the class
Give a demo directly, note the parameters.
1234567891011121314151617181920 |
package
com.aaron.reflect;
import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
public
class
Demo5 {
public
static
void
main(String[] args) {
Class<?> herosClass = Heros.
class
;
try
{
Method m1 = herosClass.getMethod(
"setName"
,String.
class
);
Method m2 = herosClass.getMethod(
"getName"
);
Object userInfo = herosClass.newInstance();
m1.invoke(userInfo,
"影魔"
);
System.out.println(
"调用set方法:"
+userInfo);
System.out.println(
"调用get方法:"
+m2.invoke(userInfo));
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
Operation Result:
12345678 |
调用set方法: Heros [ name=影魔, type= null , camp= 0 ] 调用get方法: 影魔 |
Java Reflection Mechanism Demo (v)-Get and Invoke methods in a class