Java之反射代码演示说明
生活随笔
收集整理的這篇文章主要介紹了
Java之反射代码演示说明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
還不存在的類–即我們需要使用反射來使用的類
Person類:
package com.qf.demo4;public class Person {private String name;public int age;static int weight;int male;protected int num;public Person(String name, int age, int weight) {this.name = name;this.age = age;this.weight = weight;System.out.println("有參數的構造方法");}public Person() {System.out.println("無參數的構造方法");}public void eat() {System.out.println("吃");}public void drink(String type) {System.out.println("喝" + type);}public String sleep() {System.out.println("睡");return "呵呵";}private void play() {System.out.println("玩");}public static void hehe() {System.out.println("heheheheh");}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ",weight = "+weight+"]";} }演示代碼:
Test1:創建Class對象
package com.qf.demo4;import com.qf.demo.Person; /*** 每一個類只有一個class 對象* com.qf.demo.Person 包名+類名 全限定名* @author Administrator**/ public class Test {public static void main(String[] args) {// 創建Class 對象// 第一種方式:類.classClass class1 = Person.class;System.out.println(class1.hashCode());// 第二種方式:創建類的對象;然后--對象.getClass()Person person = new Person();Class class2 = person.getClass();System.out.println(class2.hashCode());// 第三種方式:使用forname方法,參數為相對于本工程的地址try {Class class3 = Class.forName("com.qf.demo.Person");System.out.println(class3.hashCode());} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }Test2:創建Constructor對象,并創建Person類(還沒有的類)對象
package com.qf.demo4;import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException;public class Test2 {public static void main(String[] args) {try {Class class1 = Class.forName("com.qf.demo4.Person");Constructor[] constructors = class1.getConstructors();for (Constructor constructor : constructors) {System.out.println(constructor);}// 1 通過構造方法創建 Perosn對象Constructor constructor = class1.getConstructor(null);// 得到無參的構造方法Person person = (Person) constructor.newInstance(null);System.out.println(person);// 2 利用有參數的構造方法創建對象Constructor constructor2 = class1.getConstructor(String.class,int.class,int.class);Person person2 = (Person) constructor2.newInstance("張三",5,9);System.out.println(person2);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }Test3:獲取方法,以及調用方法
package com.qf.demo4;import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;public class Test3 {public static void main(String[] args) {try {Class class1 = Class.forName("com.qf.demo4.Person");// 得到構造方法Constructor constructor = class1.getConstructor(null);Object object = constructor.newInstance(null);Method[] methods = class1.getMethods();// 得到本類和父類的非私有的方法Method[] methods2 = class1.getDeclaredMethods();// 得到本類中聲明的所有的方法(包括私有)// 得到 無參 無返回值的 方法并且調用Method method = class1.getMethod("eat", null);// 調用方法的對象 實際參數method.invoke(object, null);// 是哪個方法被調用// 調用 有參數 無返回值的方法Method method2 = class1.getMethod("drink", String.class);method2.invoke(object, "水");// 有返回值的Method method3 = class1.getMethod("sleep", null);Object object2 = method3.invoke(object, null);// invoke方法的返回值就是調用的方法的返回值System.out.println(object2);// 得到靜態的方法Method method4 = class1.getMethod("hehe", null);method4.invoke(null, null);// 靜態方法不需要傳遞對象 直接寫null// 私有的 私有的方法 必須用 getDeclaredMethod 才能得到Method method5 = class1.getDeclaredMethod("play", null);method5.setAccessible(true);// 私有方法默認沒有權限調用,必須單獨設定調用權限method5.invoke(object, null);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }反射定義:http://blog.csdn.net/baidu_37107022/article/details/71234940
總結
以上是生活随笔為你收集整理的Java之反射代码演示说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生成JSON数据--fastjson(阿
- 下一篇: Java之反射--练习