java 反射构造函数_浅谈Java反射机制 之 使用类的 属性、方法和构造函数

本文深入探讨Java反射机制,包括如何使用反射获取类属性、方法及构造函数,并通过示例代码展示了不同访问权限下成员的获取与操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面两篇我们总结了Java反射机制如何获取类的字节码,如何获取构造函数,属性和方法,

这篇我们将进一步验证如何使用我们获取到的属性、方法以及构造函数

1、使用 反射 获取到的 属性

importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.Date;public classTest08 {public static void main(String[] args) throwsClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//加载Class对象//会报出不存在该类的异常

Class c=Class.forName("com.reflection.model.Student");//类名

System.out.println("类名:"+c.getSimpleName());//类的包名

System.out.println("类的包名:"+c.getPackage());

System.out.println("================使用获取到的字段=================");//根据字段名获取字段

Object object=c.getConstructor().newInstance();//获取公共字段

Field field1=c.getField("name");//设置字段值

field1.set(object,"Not_Copy");

System.out.println("-----------------"+field1.getName()+"-------------");

System.out.println("name字段的值:"+field1.get(object));

System.out.println("name字段类型:"+field1.getType());

System.out.println("name字段修饰符:"+field1.getModifiers());//私有字段

Field field2=c.getDeclaredField("loginName");//开启 private 权限的方问权限

field2.setAccessible(true);//设置字段值

field2.set(object,"Login_Not_Copy");

System.out.println("-----------------"+field2.getName()+"-------------");

System.out.println("loginName字段的值:"+field2.get(object));

System.out.println("loginName字段类型:"+field2.getType());

System.out.println("loginName字段修饰符:"+field2.getModifiers());//私有字段

Field field3=c.getDeclaredField("age");//开启 protected 权限的方问权限

field3.setAccessible(true);//设置字段值

field3.set(object,23);

System.out.println("-----------------"+field3.getName()+"-------------");

System.out.println("age字段的值:"+field3.get(object));

System.out.println("age字段类型:"+field3.getType());

System.out.println("age字段修饰符:"+field3.getModifiers());

Field field4=c.getDeclaredField("Birthday");//私有字段//开启 默认 权限的方问权限

field4.setAccessible(true);//设置字段值

field4.set(object,newDate());

System.out.println("-----------------"+field4.getName()+"-------------");

System.out.println("Birthday字段的值:"+field4.get(object));

System.out.println("Birthday字段类型:"+field4.getType());

System.out.println("Birthday字段修饰符:"+field4.getModifiers());}

}

结果:

类名:Student

类的包名:package com.reflection.model

================使用获取到的字段=================

-----------------name-------------

name字段的值:Not_Copy

name字段类型:class java.lang.String

name字段修饰符:1

-----------------loginName-------------

loginName字段的值:Login_Not_Copy

loginName字段类型:class java.lang.String

loginName字段修饰符:2

-----------------age-------------

age字段的值:23

age字段类型:class java.lang.Integer

age字段修饰符:4

-----------------Birthday-------------

Birthday字段的值:Mon Jul 22 21:28:02 CST 2019

Birthday字段类型:class java.util.Date

Birthday字段修饰符:0

结论:由代码 反射获取到的属性的  private、private 和 默认权限 都需要使用 暴力反射 ( .setAccessible(true) )来开启范围权限,才能访问。

2、使用 反射 获取到的 方法

importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.Date;public classTest08 {public static void main(String[] args) throwsClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//加载Class对象//会报出不存在该类的异常

Class c=Class.forName("com.reflection.model.Student");System.out.println("================使用获取到的方法=================");

Method method1=c.getMethod("method1", String.class);//执行方法

Object invoke1 =method1.invoke(object,"method1");

System.out.println("-----------------"+method1.getName()+"-------------");

System.out.println("method1方法的返回参数列表:"+method1.getParameterTypes());

System.out.println("method1字段修饰符:"+method1.getModifiers());

System.out.println("method1字段修饰符:"+method1.getReturnType());

System.out.println("方法的返回值:"+invoke1);

Method method2=c.getDeclaredMethod("method2");//开启private权限的方问权限

method2.setAccessible(true);//执行方法

Object invoke2 =method2.invoke(object,null);

System.out.println("-----------------"+method2.getName()+"-------------");

System.out.println("method2方法的返回参数列表:"+method2.getParameterTypes());

System.out.println("method2字段修饰符:"+method2.getModifiers());

System.out.println("method2字段修饰符:"+method2.getReturnType());

System.out.println("方法的返回值:"+invoke2);

Method method3=c.getDeclaredMethod("method3", String.class, Integer.class, Date.class);//开启默认权限的方问权限

method3.setAccessible(true);//执行方法

Object invoke3= method3.invoke(object,"No_Copy",23,newDate());

System.out.println("-----------------"+method3.getName()+"-------------");

System.out.println("method3方法的返回参数列表:"+method3.getParameterTypes());

System.out.println("method3字段修饰符:"+method3.getModifiers());

System.out.println("method3字段修饰符:"+method3.getReturnType());

System.out.println("方法的返回值:"+invoke3);

Method method4=c.getDeclaredMethod("method4");//开启 protected 权限的方问权限

method4.setAccessible(true);//执行方法

Object invoke4= method4.invoke(object,null);

System.out.println("-----------------"+method4.getName()+"-------------");

System.out.println("method4方法的返回参数列表:"+method4.getParameterTypes());

System.out.println("method4字段修饰符:"+method4.getModifiers());

System.out.println("method4字段修饰符:"+method4.getReturnType());

System.out.println("方法的返回值:"+invoke4);

}

}

结果:

================使用获取到的方法=================

public 修饰的方法:method1

-----------------method1-------------

method1方法的返回参数列表:[Ljava.lang.Class;@14ae5a5

method1字段修饰符:1

method1字段修饰符:class java.lang.String

方法的返回值:method1

private 修饰的方法:method2

-----------------method2-------------

method2方法的返回参数列表:[Ljava.lang.Class;@7f31245a

method2字段修饰符:2

method2字段修饰符:class java.lang.String

方法的返回值:method2

protected 修饰的方法 method3

name:No_Copy age:23 birthday:Mon Jul 22 21:28:02 CST 2019

-----------------method3-------------

method3方法的返回参数列表:[Ljava.lang.Class;@6d6f6e28

method3字段修饰符:0

method3字段修饰符:class java.lang.String

方法的返回值:No_Copy 23 Mon Jul 22 21:28:02 CST 2019

protected 修饰的方法:method4

-----------------method4-------------

method4方法的返回参数列表:[Ljava.lang.Class;@135fbaa4

method4字段修饰符:4

method4字段修饰符:class java.lang.String

方法的返回值:method4

结论:由代码 反射获取到方法 的  private、private 和 默认权限 都需要使用 暴力反射 ( .setAccessible(true) )来开启范围权限,才能访问。

2、使用 反射 获取到的 构造函数

importcom.reflection.model.Student;importjava.lang.reflect.Constructor;importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.Date;public classTest08 {public static void main(String[] args) throwsClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//加载Class对象//会报出不存在该类的异常

Class c=Class.forName("com.reflection.model.Student");

System.out.println(Student.class.getSuperclass().getName());System.out.println("================使用获取到的构造方法=================");//调用默认的构造方法

Object object1=c.getConstructor().newInstance();//指定要调用构造方法的参数 参数:指定每个参数的类型

Constructor constructor = c.getConstructor(String.class,String.class,Integer.class,Date.class);//利用构造constructor创建新实例 并传参调用此构造函数

Student stu = (Student) constructor.newInstance("Not_Copy","not_copy",25,newDate());

System.out.println(stu);

}

}

结果:

================使用获取到的构造方法=================

Student{name='Not_Copy', loginName='not_copy', age=25, Birthday=Mon Jul 22 21:49:12 CST 2019}

贴上 使用的 Student  和  People类:

People类

public classPeople {publicString head;publicString foot;

}

Student类:

importjava.util.Date;public class Student extendsPeople{publicString name;privateString loginName;protectedInteger age;

Date Birthday;publicStudent(){super();

}privateStudent(String name){this.name=name;

}privateStudent(Integer age){this.age=age;

}privateStudent(Date Birthday){this.Birthday=Birthday;

}publicStudent(String name,Integer age){this.name=name;this.age=age;

}publicStudent(Integer age,String name){this.name=name;this.age=age;

}publicStudent(String name,Date Birthday){this.name=name;this.Birthday=Birthday;

}publicStudent(Date Birthday,String name){this.name=name;this.Birthday=Birthday;

}publicStudent(Integer age,Date Birthday){this.age=age;this.Birthday=Birthday;

}publicStudent(Date Birthday,Integer age){this.age=age;this.Birthday=Birthday;

}publicStudent(String name,String loginName){this.name=name;this.loginName=loginName;

}publicStudent(String name,Integer age,Date Birthday){this.age=age;this.name=name;this.Birthday=Birthday;

}publicStudent(String name,String loginName,Integer age,Date Birthday){this.name=name;this.loginName=loginName;this.age=age;this.Birthday=Birthday;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}publicDate getBirthday() {returnBirthday;

}public voidsetSex(Date sex) {this.Birthday =Birthday;

}publicString method1(String str){

System.out.println("public 修饰的方法:"+str);returnstr;

}privateString method2(){

System.out.println("private 修饰的方法:"+"method2");return "method2";

}

String method3(String name,Integer age,Date birthday){

System.out.println("protected 修饰的方法 method3 \n name:"+name+" age:"+age+" birthday:"+birthday);return name+" "+age+" "+birthday;

}protectedString method4(){

System.out.println("protected 修饰的方法:"+"method4");return "method4";

}

@OverridepublicString toString() {return "Student{" +

"name='" + name + '\'' +

", loginName='" + loginName + '\'' +

", age=" + age +

", Birthday=" + Birthday +

'}';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值