java基础之反射一 基础API

   最近在开发过程中,用到好多反射的知识点,本篇文章复习下反射的内容。

一、创建反射的对象Student

复制代码
  1 import com.anno.CustomAnno;
  2 
  3 public class Student {
  4     
  5     @CustomAnno(required = true, length = 30)
  6     private String name;
  7     
  8     @CustomAnno(required = true, length = 3)
  9     private Integer age;
 10     
 11     @CustomAnno(required = false, length = 30)
 12     private String sex;
 13     
 14     @CustomAnno(required = false, length = 20)
 15     public double weight;
 16     
 17     @CustomAnno(required = false, length = 20)
 18     protected double height;
 19 
 20     public Student() {
 21         super();
 22     }
 23     
 24     private Student(String name, Integer age) {
 25         super();
 26         this.name = name;
 27         this.age = age;
 28     }
 29 
 30     public Student(String name, Integer age, String sex) {
 31         super();
 32         this.name = name;
 33         this.age = age;
 34         this.sex = sex;
 35     }
 36     
 37     public Student(String name, Integer age, String sex, double weight, double height) {
 38         super();
 39         this.name = name;
 40         this.age = age;
 41         this.sex = sex;
 42         this.weight = weight;
 43         this.height = height;
 44     }
 45 
 46     public String getName() {
 47         return name;
 48     }
 49 
 50     public void setName(String name) {
 51         this.name = name;
 52     }
 53 
 54     public Integer getAge() {
 55         return age;
 56     }
 57 
 58     public void setAge(Integer age) {
 59         this.age = age;
 60     }
 61 
 62     public String getSex() {
 63         return sex;
 64     }
 65 
 66     public void setSex(String sex) {
 67         this.sex = sex;
 68     }
 69     
 70     public double getWeight() {
 71         return weight;
 72     }
 73 
 74     public void setWeight(double weight) {
 75         this.weight = weight;
 76     }
 77     
 78     public double getHeight() {
 79         return height;
 80     }
 81 
 82     public void setHeight(double height) {
 83         this.height = height;
 84     }
 85 
 86     @Override
 87     public String toString() {
 88         return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
 89     }
 90     
 91     public void show() {
 92         System.out.println("show()  void");
 93     }
 94     
 95     private String result() {
 96         return "result  String";
 97     }
 98     
 99     private String result(String result) {
100         return result;
101     }
102 }
View Code
复制代码

二、反射有关的基本Api

复制代码
 1 import java.lang.reflect.Constructor;
 2 import java.lang.reflect.Field;
 3 import java.lang.reflect.Method;
 4 
 5 public class StudentReflect {
 6     public static void main(String[] args) throws Exception {
 7         try {
 8             Class<?> clazz = Class.forName("com.reflect.Student");
 9             //获取构造器
10             System.out.println("----------------获取默认无参构造器:getDeclaredConstructor()---------------");
11             Constructor<?> defaultConstructor = clazz.getDeclaredConstructor();
12             // 根据构造器创建实例对象
13             Student consInstance = (Student) defaultConstructor.newInstance();
14             consInstance.setName("李四");
15             System.out.println("默认构造器初始化实例对象:" + consInstance);
16             
17             System.out.println("----------------获取public修饰的有参构造器:getDeclaredConstructor(Class<?>...)----------");
18             // 注意参数类型的顺序与构造器中的顺序保持一致
19             Constructor<?> constructor = clazz.getDeclaredConstructor(String.class, Integer.class, String.class);
20             Object instanceByConstructor = constructor.newInstance("王五", 12, "男");
21             System.out.println("有参构造器初始化实例对象:" + instanceByConstructor);
22             
23             System.out.println("----------------获取private修饰的有参构造器:getDeclaredConstructor(Class<?>...)----------");
24             Constructor<?> privateConstruct = clazz.getDeclaredConstructor(String.class, Integer.class);
25             privateConstruct.setAccessible(true);
26             Object instanceByPrivateConstruct = privateConstruct.newInstance("麻六", 15);
27             System.out.println("私有有参构造函数初始化实例对象:" + instanceByPrivateConstruct);
28             
29             
30             // 获取指定的属性名称
31             System.out.println("----------------获取指定的属性:getDeclaredField(var)---------------");
32             Field nameField = clazz.getDeclaredField("name");
33             System.out.println("获取指定属性name的类型:" + nameField.getType());
34             // 获取对象实例
35             System.out.println("-------------------------实例对象属性赋值----------------------------");
36             Object instance = clazz.newInstance();
37             // name为私有属性,设置允许访问
38             nameField.setAccessible(true);
39             // 给实例对象的name属性赋值
40             nameField.set(instance, "张三");
41             System.out.println("instance实例被赋值后:" + instance);
42             System.out.println("--------------------获取public修饰的属性:getFields()--------------------");
43             // getFields() 仅能获取public修饰的属性
44             Field[] publicFields = clazz.getFields();
45             for (Field field : publicFields) {
46                 System.out.println("获取public修饰的属性名称:" + field.getName());
47             }
48             System.out.println("-----------------获取所有的属性:getDeclaredFields()---------------------");
49             Field[] fields = clazz.getDeclaredFields();
50             System.out.print("获取所有的属性名称:");
51             for (Field field : fields) {
52                 field.setAccessible(true);
53                 System.out.print(field.getName() + "   ");
54             }
55             System.out.println();
56             
57             // 获取指定方法
58             System.out.println("----------------获取指定的无参方法及调用:getDeclaredMethod(var)---------------");
59             Method methodShow = clazz.getDeclaredMethod("show");    
60             // 无参方法的调用
61             methodShow.invoke(instance);
62             
63             System.out.println("----------获取指定的有参方法及调用:getDeclaredMethod(var, type)---------");
64             Method methodResult = clazz.getDeclaredMethod("result", String.class);
65             // 私有方法,需设置允许访问
66             methodResult.setAccessible(true);
67             String result = (String) methodResult.invoke(instance, "有参方法的反射调用");
68             System.out.println(result);
69             
70             System.out.println("----------------获取所有public修饰的方法(包含父类Object的方法):getMethods()---------------");
71             Method[] publicMethods = clazz.getMethods();
72             for (Method method2 : publicMethods) {
73                 System.out.print(method2.getName() + "   ");
74             }
75             System.out.println();
76             
77             System.out.println("----------------获取(Class对象)所有方法:getMethods()---------------");
78             Method[] allMethods = clazz.getDeclaredMethods();
79             for (Method method2 : allMethods) {
80                 System.out.print(method2.getName() + "   ");
81             }
82             
83         } catch (ClassNotFoundException e) {
84             e.printStackTrace();
85         }
86     }
87 }
View Code
复制代码

 

posted @   无虑的小猪  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示