首页 技术 正文
技术 2022年11月10日
0 收藏 631 点赞 3,158 浏览 8810 个字

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

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

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

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, 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,new Date());
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、使用 反射 获取到的 方法

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, 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,new Date());
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、使用 反射 获取到的 构造函数

import com.reflection.model.Student;import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, 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,new Date()); 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 class People {
public String head;
public String foot;
}

Student类:

import java.util.Date;public class Student extends People{    public String name;    private String loginName;    protected Integer age;    Date Birthday;    public Student(){
super();
} private Student(String name){
this.name=name;
} private Student(Integer age){
this.age=age;
} private Student(Date Birthday){
this.Birthday=Birthday;
} public Student(String name,Integer age){
this.name=name;
this.age=age;
} public Student(Integer age,String name){
this.name=name;
this.age=age;
} public Student(String name,Date Birthday){
this.name=name;
this.Birthday=Birthday;
} public Student(Date Birthday,String name){
this.name=name;
this.Birthday=Birthday;
} public Student(Integer age,Date Birthday){
this.age=age;
this.Birthday=Birthday;
} public Student(Date Birthday,Integer age){
this.age=age;
this.Birthday=Birthday;
} public Student(String name,String loginName){
this.name=name;
this.loginName=loginName;
} public Student(String name,Integer age,Date Birthday){
this.age=age;
this.name=name;
this.Birthday=Birthday;
} public Student(String name,String loginName,Integer age,Date Birthday){
this.name=name;
this.loginName=loginName;
this.age=age;
this.Birthday=Birthday;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Date getBirthday() {
return Birthday;
} public void setSex(Date sex) {
this.Birthday = Birthday;
} public String method1(String str){
System.out.println("public 修饰的方法:"+str);
return str;
} private String 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;
} protected String method4(){
System.out.println("protected 修饰的方法:"+"method4");
return "method4";
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", loginName='" + loginName + '\'' +
", age=" + age +
", Birthday=" + Birthday +
'}';
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292