java对方法进行反射

1.获取类的class信息

2.获取method

3.invoke方法执行method

 

被反射对象

```
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String id;
private String username;
private String gongzhong;
private String starttime;
private String score;
private String progress;


}
```

 

反射方法

```
Class clazz = User.class;
User user = new User();
Method method = clazz.getMethod("setUsername",String.class);
method.invoke(user,"123");
log.info(user.toString());
```

posted @ 2023-02-01 11:18  yorkiiz  阅读(15)  评论(0编辑  收藏  举报