日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

反射的实现

發(fā)布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 反射的实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.獲取類的字節(jié)碼對象
//獲取類的字節(jié)碼對象 public class Demo1 {public static void main(String[] args) throws ClassNotFoundException {//方式1:類名.ClassClass<?> clazz1 = Student.class;//方式2:對象名.getClass()Student student = new Student();Class<?> clazz2 = student.getClass();//方式3:Class.forName("類的全路徑名")Class<?> clazz3 = Class.forName("com.itheima1.Student");System.out.println(clazz1);System.out.println(clazz1 == clazz2);System.out.println(clazz2 == clazz3);} }
2.獲取Constructor【構(gòu)造方法】對象

學(xué)生類

public class Student {private String name;private int age;//公共的無參構(gòu)造方法public Student() {System.out.println("public...Student...無參構(gòu)造方法");}//公共的有參構(gòu)造方法public Student(String name, int age) {System.out.println("name的值為:" + name + "age的值為" + age);System.out.println("public...Student...有參構(gòu)造方法");}//私有的有參構(gòu)造方法private Student(String name) {this.name= name ;System.out.println("name的值為:" + name);System.out.println("private...Student...有參構(gòu)造方法");}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';} }

獲取Constructor對象的方法

//獲取Constructor對象 public class Demo1 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {//首先得到Class對象再調(diào)用方法Class<?> clazz = Class.forName("com.itheima2.Student");//Constructor<T> getConstructor (class<?>... parameterTypes):返回單個公共構(gòu)造方法對象【只能返回公共的,默認(rèn)的等也不行】//Constructor<?> constructor = clazz.getConstructor();//constructor<T> getDeclaredConstructor (class<?>... parameterTypes):返回單個構(gòu)造方法對象【可以返回私有的】Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class, int.class);System.out.println(declaredConstructor);//Constructor<?>[] getConstructors ():返回所有公共構(gòu)造方法對象的數(shù)組Constructor<?>[] constructors = clazz.getConstructors();for (Constructor<?> constructor : constructors) {System.out.println(constructor);}//Constructor<?>[] getDeclaredConstructors ():返回所有構(gòu)造方法對象的數(shù)組//Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();} }打印結(jié)果: ------------------------------------------------------------------------------------- public com.itheima2.Student(java.lang.String,int) public com.itheima2.Student(java.lang.String,int) public com.itheima2.Student()
2.1 Constructor對象的使用

根據(jù)指定的構(gòu)造方法創(chuàng)建對象:
Class.newInstance();只能反射無參的構(gòu)造器,需要構(gòu)造器可見;
Constructor.newInstance();可以反射任何構(gòu)造器,可以反射私有構(gòu)造器

//反射獲取Constructor對象并使用 //T newInstance (object. . . initargs):根據(jù)指定的構(gòu)造方法創(chuàng)建對象 public class Demo2 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//1.獲取Class對象Class<?> clazz = Class.forName("com.itheima2.Student");//2.獲取構(gòu)造方法對象【四種方法】Constructor<?> constructor1 = clazz.getDeclaredConstructor(String.class);//訪問私有【其他修飾都能使用】的構(gòu)造不能直接使用,getDeclaredConstructor()只能做訪問//constructor.setAccessible(true):在創(chuàng)建學(xué)生對象之前臨時取消訪問檢查【暴力反射】constructor1.setAccessible(true);//3.利用newInstance創(chuàng)建Student的對象Student student = (Student) constructor1.newInstance("張三");System.out.println(student);System.out.println("---------------------------------------------------------");//簡寫格式【不能獲取私有方法,其他都可,默認(rèn)訪問空參構(gòu)造】//1.獲取Class對象Class<?> clazz1 = Class.forName("com.itheima2.Student");//2.在Class類中,有一個newInstance方法,可以利用空參直接創(chuàng)建一個對象【已過時,做了解】Student student1 = (Student) clazz1.newInstance();System.out.println(student1);} }c name的值為:張三 private...Student...有參構(gòu)造方法 Student{name='張三', age=0} --------------------------------------------------------- public...Student...無參構(gòu)造方法 Student{name='null', age=0}
3.獲取Field【成員變量】對象

學(xué)生類

public class Student {public String name;public int age;public String gender;private int money = 300; }

獲取Field【成員變量】對象的方法

//獲取Field【成員變量】對象的方法 public class Demo1 {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {Class<?> clazz = Class.forName("com.itheima4.Student");//Field[] getFields ():返回所有公共成員變量對象的數(shù)組//Field[] fields = clazz.getFields();//Field[] getDeclaredFields ():返回所有成員變量對象的數(shù)組Field[] declaredFields = clazz.getDeclaredFields();for (Field declaredField : declaredFields) {System.out.println(declaredField);}// Field getField (string name):返回單個公共成員變量對象// 想要獲取的成員變量必須是真實(shí)存在的//且必須是public修飾的Field f1 = clazz.getField("name");System.out.println(f1);// Field getDeclaredField (string name):返回單個成員變量對象Field f2 = clazz.getDeclaredField("money");System.out.println(f2);} }打印結(jié)果: ------------------------------------------------------------------------------------- public java.lang.String com.itheima4.Student.name public int com.itheima4.Student.age public java.lang.String com.itheima4.Student.gender private int com.itheima4.Student.money public java.lang.String com.itheima4.Student.name private int com.itheima4.Student.money
3.1 Field對象的使用

根據(jù)指定的構(gòu)造方法創(chuàng)建對象:
void set (object obj, object value):給obj對象的成員變量賦值為value【如果私有則需要暴力反射】
object get (object obj)返回由該 Field表示的字段在指定對象上的值

//利用Field對象,獲取值或者修改值 public class Demo2 {public static void main(String[] args) throws ReflectiveOperationException {// 1.獲取class對象Class<?> clazz = Class.forName("com.itheima4.Student");//2.獲取name這個Field對象Field f1 = clazz.getDeclaredField("name");Field f2 = clazz.getDeclaredField("age");Field f3 = clazz.getDeclaredField("money");f3.setAccessible(true);f1.setAccessible(true);//由于私有則需要暴力反射[在對象創(chuàng)建之前]//創(chuàng)建學(xué)生類的對象【如果空參構(gòu)造私有,則要對成員方法暴力反射】Student student = (Student) clazz.getConstructor().newInstance();//3.利用set方法進(jìn)行賦值//void set (object obj, object value):給obj對象的成員變量賦值為valuef2.set(student, 20);f1.set(student, "張三");//等價于obj.setNome("張三");//4.獲取屬性值//object get (object obj)返回由該 Field表示的字段在指定對象上的值System.out.println(f1.get(student));System.out.println(f2.get(student));System.out.println(f3.get(student));} }打印結(jié)果: ------------------------------------------------------------------------------------- 張三 20 300
4 獲取Method【成員方法】對象

學(xué)生類

public class Student {//有一個參數(shù)的show方法public void show(String n) { System.out.println("show..." + n); }//有兩個參數(shù)的show方法private void show(String n, int m) {System.out.println("show..." + n +"..."+ m);}//有兩個參數(shù),有返回值的方法public int sum(int n, int m) {return m + n;} }

反射獲取學(xué)生類成員方法對象

<font color ="sky bule">反射獲取Method【成員方法】對象 public class Demo1 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {Class<?> clazz = Class.forName("com.itheima5.Student");//Method[] getMethods ():返回所有公共成員方法對象的數(shù)組,包括繼承的/*Method[] methods = clazz.getMethods();for (Method method : methods) {System.out.println(method);}*///Method[] getDeclaredMethods ():返回所有成員防法對象的數(shù)組,不包括繼承的Method[] declaredMethods = clazz.getDeclaredMethods();for (Method declaredMethod : declaredMethods) {System.out.println(declaredMethod);}//Method getMethod (string name,class<?>... parameterTypes) :返回單個公共成員方法對象//Method getDeclaredMethod (string name,Class<?>... parameterTypes):返回單個成員方法對象Method show = clazz.getDeclaredMethod("show", String.class, int.class);System.out.println(show);} }打印結(jié)果: ------------------------------------------------------------------------------------- public int com.itheima5.Student.sum(int,int) public void com.itheima5.Student.show(java.lang.String) private void com.itheima5.Student.show(java.lang.String,int) private void com.itheima5.Student.show(java.lang.String,int)
4.1 Method對象的使用

method.invoke(student, 200, 100);

//反射獲取成員方法使用 public class Demo2 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {//獲取類的字節(jié)碼文件Class<?> clazz = Class.forName("com.itheima5.Student");//創(chuàng)建一個學(xué)生對象【調(diào)用方法使用】Student student = (Student) clazz.getConstructor().newInstance();//獲取一個參數(shù)的show方法Method m1 = clazz.getMethod("show" , String.class);//System.out.println(m1);//執(zhí)行m1.invoke(student,"hahah");//等價于obj.show (" hahah");//獲取兩個參數(shù)的show方法Method m2 = clazz.getDeclaredMethod("show", String.class, int.class);//System.out.println(m2);m2.setAccessible(true);//執(zhí)行m2.invoke(student,"hehe",100);//等價于obj.show ("hehe",100);//獲取兩個int參數(shù)的sum方法[名稱參數(shù)必須一致,不能少]Method m3 = clazz.getMethod("sum",int.class,int.class);//System.out.println(m3);//執(zhí)行int sum = (int) m3.invoke(student, 200, 100);System.out.println(sum);} }打印結(jié)果: ------------------------------------------------------------------------------------- show...hahah show...hehe...100 300
5. 思路總結(jié)

<1>獲取Constructor對象
1.創(chuàng)建Class方法【Class.forName(“類的全路徑名”)】—通用

2.調(diào)用Class獲取Constructor對象方法
先看修飾符
如果需要訪問私有,則用帶Declared的方法
否則兩種類型的方法都可以使用

3.訪問單個構(gòu)造:看參數(shù)類型和個數(shù)填寫方法參數(shù)得到Constructor對象
訪問多個構(gòu)造:直接調(diào)用方法得到Constructor對象

4.得到Constructor對象后使用
Class.newInstance();只能反射無參的構(gòu)造器,需要構(gòu)造器非私有;
Constructor.newInstance();可以反射任何構(gòu)造器,可以反射私有構(gòu)造器【創(chuàng)建學(xué)生對象前加取消訪問檢查方法】

<2>Constructor.newInstance()得到的對象傳遞到 Field對象調(diào)用的set();get();參數(shù)中執(zhí)行方法
<3>Constructor.newInstance()得到的對象傳遞到Method對象調(diào)用的invoke();參數(shù)中執(zhí)行方法

總結(jié)

以上是生活随笔為你收集整理的反射的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。