From eff69d6b450857756c4022f24ac0b1a01fb88517 Mon Sep 17 00:00:00 2001 From: object <815363734@qq.com> Date: Fri, 11 Nov 2016 11:53:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B7=B2=E8=A2=AB=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20Signed-off-by:=20object=20<815363734@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaCore/.classpath | 2 +- JavaCore/.gitignore | 5 +- .../com/ddb/javacore/reflect/ClassDemo.java | 153 +++++++++--------- 3 files changed, 81 insertions(+), 79 deletions(-) diff --git a/JavaCore/.classpath b/JavaCore/.classpath index a91a716..bab95b2 100644 --- a/JavaCore/.classpath +++ b/JavaCore/.classpath @@ -2,6 +2,6 @@ - + diff --git a/JavaCore/.gitignore b/JavaCore/.gitignore index e1453be..6b07b34 100644 --- a/JavaCore/.gitignore +++ b/JavaCore/.gitignore @@ -1,3 +1,4 @@ /bin/ - -*.class \ No newline at end of file +.classpath +*.class +.gitignore \ No newline at end of file diff --git a/JavaCore/src/com/ddb/javacore/reflect/ClassDemo.java b/JavaCore/src/com/ddb/javacore/reflect/ClassDemo.java index f1c98a1..1e99ea8 100644 --- a/JavaCore/src/com/ddb/javacore/reflect/ClassDemo.java +++ b/JavaCore/src/com/ddb/javacore/reflect/ClassDemo.java @@ -1,76 +1,77 @@ -package com.ddb.javacore.reflect; - -import java.io.PrintStream; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -public class ClassDemo { - - public static void main(String[] args) - throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, - IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { - PrintStream out = System.out; - // 第一种得到Class 实例的方式 - Class class1 = Class.forName("com.ddb.javacore.reflect.Person"); - // 第二种得到Class 实例的方式 - Class class2 = Person.class; - // 第三种得到Class 实例的方式 - Class class3 = new Person().getClass(); - - out.println(class1); - out.println(class2); - out.println(class3); - - out.println(class1.getName()); - out.println(class2.getName()); - out.println(class3.getName()); - - out.println("class1.getComponentType() : " + class1.getComponentType()); - out.println("class1.getModifiers() :" + class1.getModifiers()); - out.println("class1.getSimpleName() :" + class1.getSimpleName()); - out.println("class1.getTypeName() :" + class1.getTypeName()); - out.println("class1.getClassLoader() : " + class1.getClassLoader()); - out.println("class1.getDeclaredMethods() :"); - out.println("该类拥有的方法:"); - Method[] methods = class1.getDeclaredMethods(); - for (Method method : methods) { - out.println("method : " + method); - } - - Field[] fields = class1.getDeclaredFields(); - out.println("该类拥有的字段:"); - for (Field field : fields) { - out.println("field : " + field); - } - - // 利用反射创建Person类的实例 - Person obj = (Person) class1.newInstance(); - - // 访问指定的字段 - out.println("访问指定的字段 :age"); - Field fname = class1.getDeclaredField("name"); - Field fage = class1.getDeclaredField("age"); - - fname.setAccessible(true); - out.println("fname.get(obj) :" + fname.get(obj)); - fage.setAccessible(true); - out.println("fage.get(obj) :" + fage.get(obj)); - - out.println("通过反射设定属性:"); - fname.set(obj, "zhangsan01"); - fage.set(obj, 32); - out.println("fname.get(obj) :" + fname.get(obj)); - fage.setAccessible(true); - out.println("fage.get(obj) :" + fage.get(obj)); - out.println("obj.toString() :" + obj.toString()); - - out.println("通过反射唤醒或执行被反射对象的方法:"); - Method method = class1.getMethod("printPersonSelf"); - - out.println("唤醒printPersonSelf:"); - method.invoke(obj); - - } - -} +package com.ddb.javacore.reflect; + +import java.io.PrintStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class ClassDemo { + + public static void main(String[] args) + throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, + IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { + PrintStream out = System.out; + // 第一种得到Class 实例的方式 + Class class1 = Class.forName("com.ddb.javacore.reflect.Person"); + // 第二种得到Class 实例的方式 + Class class2 = Person.class; + // 第三种得到Class 实例的方式 + Class class3 = new Person().getClass(); + + out.println(class1); + out.println(class2); + out.println(class3); + + out.println(class1.getName()); + out.println(class2.getName()); + out.println(class3.getName()); + + out.println("class1.getComponentType() : " + class1.getComponentType()); + out.println("class1.getModifiers() :" + class1.getModifiers()); + out.println("class1.getSimpleName() :" + class1.getSimpleName()); + out.println("class1.getTypeName() :" + class1.getTypeName()); + out.println("class1.getClassLoader() : " + class1.getClassLoader()); + out.println("class1.getDeclaredMethods() :"); + out.println("该类拥有的方法:"); + Method[] methods = class1.getDeclaredMethods(); + for (Method method : methods) { + out.println("method : " + method); + } + + Field[] fields = class1.getDeclaredFields(); + out.println("该类拥有的字段:"); + for (Field field : fields) { + out.println("field : " + field); + } + + // 利用反射创建Person类的实例 + Person obj = (Person) class1.newInstance(); + + // 访问指定的字段 + out.println("访问指定的字段 :age"); + Field fname = class1.getDeclaredField("name"); + Field fage = class1.getDeclaredField("age"); + + fname.setAccessible(true); + out.println("fname.get(obj) :" + fname.get(obj)); + fage.setAccessible(true); + out.println("fage.get(obj) :" + fage.get(obj)); + + out.println("通过反射设定属性:"); + fname.set(obj, "zhangsan01"); + fage.set(obj, 32); + out.println("fname.get(obj) :" + fname.get(obj)); + fage.setAccessible(true); + out.println("fage.get(obj) :" + fage.get(obj)); + out.println("obj.toString() :" + obj.toString()); + + out.println("通过反射唤醒或执行被反射对象的方法:"); + Method method = class1.getMethod("printPersonSelf"); + + out.println("唤醒printPersonSelf:"); + method.invoke(obj); + + + } + +}