Home  >  Article  >  Java  >  How to use reflection API to manipulate information in java

How to use reflection API to manipulate information in java

PHPz
PHPzforward
2023-04-30 13:34:06774browse

1. Obtaining the class object you want to operate is the core of reflection. We can call any class method through the class object.

2. Calling the class method is the use stage of reflection.

3. Use reflection API to operate this information.

Example

class Worker{
/*两个public构造方法*/
public Worker(){
count++;
}
public Worker(String name){
super();
this.name = name;
}
/*两个private构造方法*/
private Worker(String name,int age){
super();
this.name = name;
this.age = age;
}
private Worker(int age){
super();
this.age = age;
}
/*两个private属性*/
private String name;
private int age;
/*一个public属性*/
public String parents;
/*一个private static属性*/
private static int count;
/*一个 public static属性*/
public static String address;
/*两个public方法*/
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
/*两个private方法*/
private int getAge(){
return age;
}
private void setAge(int age){
this.age = age;
}
/*两个public static方法*/
public static void printAddress(String address){
System.out.println("printAddress==="+address);
}
public static void printAddress(){
System.out.println("printAddress===default");
}
/*两个private static方法*/
private static void printlnCount(int count){
System.out.println("printCount==="+count);
}
private static void printlnCount(){
System.out.println("printCount===000");
}
@Override
public String toString(){
return "这是一个Worker实例:name="+name+",age="+age;
}
}

The above is the detailed content of How to use reflection API to manipulate information in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete