From b40e947b176217eb857c3ae0760af6b85641813e Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Sat, 24 Sep 2022 00:05:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0PropertyDescriptor=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../asgc/neutrino/core/bean/test2/Test1.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java index 985879c0..0cce39f6 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test2/Test1.java @@ -21,13 +21,11 @@ */ package fun.asgc.neutrino.core.bean.test2; -import lombok.Data; import org.junit.Test; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; /** * @@ -38,27 +36,38 @@ public class Test1 { @Test public void test1() throws IntrospectionException, InvocationTargetException, IllegalAccessException { - PropertyDescriptor descriptor = new PropertyDescriptor("age", Student.class); - Student student = new Student(); - Method method = descriptor.getWriteMethod(); - method.invoke(student, 30); - System.out.println(student); + PropertyDescriptor pd1= new PropertyDescriptor("name", Person.class); + PropertyDescriptor pd2= new PropertyDescriptor("age", Person.class, "getAge", "setAge"); + + Person person = new Person(); + person.setName("张三"); + + pd1.getWriteMethod().invoke(person, "李四"); + + System.out.println(pd1.getReadMethod().invoke(person)); + + pd2.getWriteMethod().invoke(person, 20); + System.out.println(pd2.getReadMethod().invoke(person)); } - public static class Student { + public static class Person { private String name; private int age; - private int score; + + public String isName() { + return name; + } public int getAge() { return age; } -// public void setAge(int age) { -// this.age = age; -// } + public void setName(String name) { + this.name = name; + } + public void setAge(int age) { - System.out.println("111"); + this.age = age; } } }