java-通过反射获取目标类的属性,方法,构造器

首先定义一个urse

 1 package com.studay_fanshe;
 2 
 3 public class User {
 4     private String uname;
 5     private int age;
 6     private int id;
 7 
 8     public User() {
 9     }
10 
11     public User(String uname, int age, int id) {
12         this.uname = uname;
13         this.age = age;
14         this.id = id;
15     }
16 
17     public String getUname() {
18         return uname;
19     }
20     public void setUname(){
21         this.uname="七月";
22     }
23     public void setUname(String uname) {
24         this.uname = uname;
25     }
26 
27     public int getAge() {
28         return age;
29     }
30 
31     public void setAge(int age) {
32         this.age = age;
33     }
34 
35     public int getId() {
36         return id;
37     }
38 
39     public void setId(int id) {
40         this.id = id;
41     }
42 }
 1 package com.studay_fanshe;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.Field;
 5 import java.lang.reflect.Method;
 6 
 7 public class test01 {
 8     public static void main(String[] args) throws Exception{
 9 
10         Class clazz = Class.forName("com.studay_fanshe.User");
11 //获取名字
12         String name = clazz.getName();//获取包名+类名:com.studay_fanshe.User
13         System.out.println(name);
14         String simpleName = clazz.getSimpleName();//获取类名:User
15         System.out.println(simpleName);
16  //获取属性
17         Field[] fields = clazz.getFields();//获取所有的公共的public修饰的属性
18         System.out.println(fields.length);
19         Field[] f = clazz.getDeclaredFields();//获取全部的属性包括私有的
20         for (Field field : f) {
21           /*  private java.lang.String com.studay_fanshe.User.uname
22               private int com.studay_fanshe.User.age
23               private int com.studay_fanshe.User.id*/
24             System.out.println(field);
25         }
26         //获取指定属性名的属性:private java.lang.String com.studay_fanshe.User.uname
27         Field uname = clazz.getDeclaredField("uname");
28         System.out.println(uname);
29 //获取方法
30         Method[] methods = clazz.getMethods();//获取指定类中所有的方法
31         for (Method method : methods) {
32             System.out.println("所有的方法:"+method);
33         }
34         //获取指定类中公有的方法
35         Method[] m = clazz.getDeclaredMethods();
36         for (Method method : m) {
37             System.out.println("public公有的方法"+method);
38         }
39         System.out.println("--------------------------------------------");
40       /*  public void setUname(String uname) {
41             this.uname = uname;
42         }*/
43       //有参数类型必须传入参数类型对应的class对象
44         Method setUname = clazz.getDeclaredMethod("setUname", String.class);
45         System.out.println(setUname);
46      /*   public void setUname(){
47             this.uname="七月";
48         }*/
49      //没有参数类型那么参2我们就写null
50         Method setUname1 = clazz.getDeclaredMethod("setUname", null);
51         System.out.println(setUname1);
52  //获得构造器
53         Constructor[] dc = clazz.getDeclaredConstructors();
54         for (Constructor constructor : dc) {
55             System.out.println("构造器:"+constructor);
56         }
57         Constructor dec = clazz.getDeclaredConstructor(String.class, int.class, int.class);
58         System.out.println("指定的构造器:"+dec);
59     }
60 }

 

转载于:https://www.cnblogs.com/xiaoqiqistudy/p/11017446.html

你可能感兴趣的:(java-通过反射获取目标类的属性,方法,构造器)