| 方法 | 说明 |
|---|---|
| void set(Object obj, Object value) | 赋值 |
| Object get(Object obj) | 获取值 |
代码示例:
- public class ReflectDemo5 {
- public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
- Student s = new Student("zhangsan",23,"广州");
- Student ss = new Student("lisi",24,"北京");
-
- //需求:
- //利用反射获取成员变量并获取值和修改值
-
- //1.获取class对象
- Class clazz = Class.forName("com.itheima.reflectdemo.Student");
-
- //2.获取name成员变量
- //field就表示name这个属性的对象
- Field field = clazz.getDeclaredField("name");
- //临时修饰他的访问权限
- field.setAccessible(true);
-
- //3.设置(修改)name的值
- //参数一:表示要修改哪个对象的name?
- //参数二:表示要修改为多少?
- field.set(s,"wangwu");
-
- //3.获取name的值
- //表示我要获取这个对象的name的值
- String result = (String)field.get(s);
-
- //4.打印结果
- System.out.println(result);
-
- System.out.println(s);
- System.out.println(ss);
-
- }
- }
-
-
- public class Student {
- private String name;
- private int age;
- public String gender;
- public String address;
-
-
- public Student() {
- }
-
- public Student(String name, int age, String address) {
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
-
- public Student(String name, int age, String gender, String address) {
- this.name = name;
- this.age = age;
- this.gender = gender;
- this.address = address;
- }
-
- /**
- * 获取
- * @return name
- */
- public String getName() {
- return name;
- }
-
- /**
- * 设置
- * @param name
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * 获取
- * @return age
- */
- public int getAge() {
- return age;
- }
-
- /**
- * 设置
- * @param age
- */
- public void setAge(int age) {
- this.age = age;
- }
-
- /**
- * 获取
- * @return gender
- */
- public String getGender() {
- return gender;
- }
-
- /**
- * 设置
- * @param gender
- */
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- /**
- * 获取
- * @return address
- */
- public String getAddress() {
- return address;
- }
-
- /**
- * 设置
- * @param address
- */
- public void setAddress(String address) {
- this.address = address;
- }
-
- public String toString() {
- return "Student{name = " + name + ", age = " + age + ", gender = " + gender + ", address = " + address + "}";
- }
- }
-
规则:
get表示获取
Declared表示私有
最后的s表示所有,复数形式
如果当前获取到的是私有的,必须要临时修改访问权限,否则无法使用
| 方法名 | 说明 |
|---|---|
| Method[] getMethods() | 返回所有成员方法对象的数组(只能拿public的) |
| Method[] getDeclaredMethods() | 返回所有成员方法对象的数组,存在就能拿到 |
| Method getMethod(String name, Class>... parameterTypes) | 返回单个成员方法对象(只能拿public的) |
| Method getDeclaredMethod(String name, Class>... parameterTypes) | 返回单个成员方法对象,存在就能拿到 |
代码示例:
- public class ReflectDemo6 {
- public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
- //1.获取class对象
- Class> clazz = Class.forName("com.itheima.reflectdemo.Student");
-
-
- //2.获取方法
- //getMethods可以获取父类中public修饰的方法
- Method[] methods1 = clazz.getMethods();
- for (Method method : methods1) {
- System.out.println(method);
- }
-
- System.out.println("===========================");
- //获取所有的方法(包含私有)
- //但是只能获取自己类中的方法
- Method[] methods2 = clazz.getDeclaredMethods();
- for (Method method : methods2) {
- System.out.println(method);
- }
-
- System.out.println("===========================");
- //获取指定的方法(空参)
- Method method3 = clazz.getMethod("sleep");
- System.out.println(method3);
-
- Method method4 = clazz.getMethod("eat",String.class);
- System.out.println(method4);
-
- //获取指定的私有方法
- Method method5 = clazz.getDeclaredMethod("playGame");
- System.out.println(method5);
- }
- }
-
方法
Object invoke(Object obj, Object... args) :运行方法
参数一:用obj对象调用该方法
参数二:调用方法的传递的参数(如果没有就不写)
返回值:方法的返回值(如果没有就不写)
代码示例:
- package com.itheima.a02reflectdemo1;
-
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
- public class ReflectDemo6 {
- public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
- //1.获取字节码文件对象
- Class clazz = Class.forName("com.itheima.a02reflectdemo1.Student");
-
- //2.获取一个对象
- //需要用这个对象去调用方法
- Student s = new Student();
-
- //3.获取一个指定的方法
- //参数一:方法名
- //参数二:参数列表,如果没有可以不写
- Method eatMethod = clazz.getMethod("eat",String.class);
-
- //运行
- //参数一:表示方法的调用对象
- //参数二:方法在运行时需要的实际参数
- //注意点:如果方法有返回值,那么需要接收invoke的结果
- //如果方法没有返回值,则不需要接收
- String result = (String) eatMethod.invoke(s, "重庆小面");
- System.out.println(result);
-
- }
- }
-
-
-
- public class Student {
- private String name;
- private int age;
- public String gender;
- public String address;
-
-
- public Student() {
-
- }
-
- public Student(String name) {
- this.name = name;
- }
-
- private Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- /**
- * 获取
- * @return name
- */
- public String getName() {
- return name;
- }
-
- /**
- * 设置
- * @param name
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * 获取
- * @return age
- */
- public int getAge() {
- return age;
- }
-
- /**
- * 设置
- * @param age
- */
- public void setAge(int age) {
- this.age = age;
- }
-
- public String toString() {
- return "Student{name = " + name + ", age = " + age + "}";
- }
-
- private void study(){
- System.out.println("学生在学习");
- }
-
- private void sleep(){
- System.out.println("学生在睡觉");
- }
-
- public String eat(String something){
- System.out.println("学生在吃" + something);
- return "学生已经吃完了,非常happy";
- }
- }