File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22<classpath >
33 <classpathentry kind =" src" path =" src" />
44 <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8" />
5- <classpathentry kind =" lib" path =" D:/eclipse/log4j-1.2.17.jar" />
6- <classpathentry kind =" lib" path =" D:/eclipse/lombok.jar" />
5+ <classpathentry kind =" lib" path =" D:/Program Files/ eclipse/log4j-1.2.17.jar" />
6+ <classpathentry kind =" lib" path =" D:/Program Files/ eclipse/lombok.jar" />
77 <classpathentry kind =" output" path =" bin" />
88</classpath >
Original file line number Diff line number Diff line change 1+ package com .xinan .reflect ;
2+
3+ public class Dog {
4+ private String name ;
5+ private int age ;
6+
7+ public Dog () {
8+ super ();
9+ }
10+
11+ public Dog (String name , int age ) {
12+ super ();
13+ this .name = name ;
14+ this .age = age ;
15+ }
16+
17+ public String getName () {
18+ return name ;
19+ }
20+
21+ public void setName (String name ) {
22+ this .name = name ;
23+ }
24+
25+ public int getAge () {
26+ return age ;
27+ }
28+
29+ public void setAge (int age ) {
30+ this .age = age ;
31+ }
32+
33+ public void TestReflect () {
34+ System .out .println ("TestReflec方法调用" );
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .xinan .reflect ;
2+
3+ public class Person {
4+ private String name ;
5+ private int age ;
6+
7+ public Person () {
8+ super ();
9+ }
10+ public Person (String name , int age ) {
11+ super ();
12+ this .name = name ;
13+ this .age = age ;
14+ }
15+ public String getName () {
16+ return name ;
17+ }
18+ public void setName (String name ) {
19+ this .name = name ;
20+ }
21+ public int getAge () {
22+ return age ;
23+ }
24+ public void setAge (int age ) {
25+ this .age = age ;
26+ }
27+ public void TestReflect (){
28+ System .out .println ("TestReflec方法调用" );
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package com .xinan .reflect ;
2+
3+ import java .lang .reflect .Constructor ;
4+ import java .lang .reflect .Field ;
5+ import java .lang .reflect .Method ;
6+ import java .lang .reflect .Modifier ;
7+
8+ public class TestDogReflect {
9+ public static void main (String [] args ) throws Exception {
10+ // Class clazz=Class.forName("com.xinan.reflect.Dog");//第一种获取类的方式
11+ // Class clazz1=Person.class;//第二种获取类的方式
12+ Class clazz2 = (new Person ().getClass ());
13+ // Field fs = clazz2.getDeclaredField("name");
14+ Field [] fs = clazz2 .getDeclaredFields ();
15+ StringBuffer sb = new StringBuffer ();
16+ sb .append (Modifier .toString (clazz2 .getModifiers ()) + " class " + clazz2 .getSimpleName () + "{\n " );
17+ for (Field f1 : fs ) {
18+ sb .append ("\t " );// 空格
19+ sb .append (Modifier .toString (f1 .getModifiers ()) + " " );// 获得属性的修饰符,例如public,static等等
20+ sb .append (f1 .getType ().getSimpleName () + " " );// 属性的类型的名字
21+ sb .append (f1 .getName () + ";\n " );// 属性的名字+回车
22+ }
23+ sb .append ("}" );
24+ System .out .println (sb );
25+ Constructor <?> cons [] = clazz2 .getConstructors ();
26+ Method [] methods = clazz2 .getDeclaredMethods ();
27+ for (Method method : methods ) {
28+ System .out .println ("me:" + method );
29+ }
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package com .xinan .reflect ;
2+
3+ public class TestIntegerReflect {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package com .xinan .reflect ;
2+
3+ import java .lang .reflect .Field ;
4+ import java .lang .reflect .Method ;
5+ import java .lang .reflect .Modifier ;
6+
7+ public class TestPerson {
8+ public static void main (String [] args ) throws Exception {
9+ Class clazz = Class .forName ("com.xinan.reflect.Person" );// 获取整个类
10+ Field [] fs = clazz .getDeclaredFields (); // 定义可变长度的字节,存储属性
11+ StringBuffer sb = new StringBuffer ();
12+ // 通过追加的方法,将每个属性拼接到此字符串中
13+ // 最外边的public定义
14+ sb .append (Modifier .toString (clazz .getModifiers ()) + " class " + clazz .getSimpleName () + "{\n " );
15+ // 里边的每一个属性
16+ for (Field field : fs ) {
17+ sb .append ("\t " );// 空格
18+ sb .append (Modifier .toString (field .getModifiers ()) + " " );// 获得属性的修饰符,例如public,static等等
19+ sb .append (field .getType ().getSimpleName () + " " );// 属性的类型的名字
20+ sb .append (field .getName () + ";\n " );// 属性的名字+回车
21+ }
22+ sb .append ("}" );
23+
24+ System .out .println (sb );
25+ Person p = (Person ) clazz .newInstance ();
26+ Field fage = clazz .getDeclaredField ("age" );
27+ fage .setAccessible (true );
28+ fage .set (p , 50 );
29+ Method method = clazz .getMethod ("TestReflect" );
30+ method .invoke (p );
31+ System .out .println ("fage.get(obj) :" + fage .get (p ));
32+ Method [] methods = clazz .getDeclaredMethods ();
33+ for (Method method1 : methods ) {
34+ System .out .println ("method : " + method1 );
35+
36+ }
37+ }
38+
39+ }
You can’t perform that action at this time.
0 commit comments