實(shí)例講解Java基礎(chǔ)之反射
前期準(zhǔn)備
編寫一個(gè)真實(shí)類phone,實(shí)現(xiàn)list接口
public class Phone implements List { public double price; public String name; public Phone() { } public Phone(double price, String name) { this.price = price; this.name = name; } public double getPrice() { return price; } public void gege(String h){ System.out.println("gege的"+h); } public void setPrice(double price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Phone{" + "price=" + price + ", name='" + name + '\'' + '}'; } @Override public int size() { return 0; } @Override public boolean isEmpty() { return false; } @Override public boolean contains(Object o) { return false; } @Override public Iterator iterator() { return null; } @Override public Object[] toArray() { return new Object[0]; } @Override public boolean add(Object o) { return false; } @Override public boolean remove(Object o) { return false; } @Override public boolean addAll(Collection c) { return false; } @Override public boolean addAll(int index, Collection c) { return false; } @Override public void clear() { } @Override public Object get(int index) { return null; } @Override public Object set(int index, Object element) { return null; } @Override public void add(int index, Object element) { } @Override public Object remove(int index) { return null; } @Override public int indexOf(Object o) { return 0; } @Override public int lastIndexOf(Object o) { return 0; } @Override public ListIterator listIterator() { return null; } @Override public ListIterator listIterator(int index) { return null; } @Override public List subList(int fromIndex, int toIndex) { return null; } @Override public boolean retainAll(Collection c) { return false; } @Override public boolean removeAll(Collection c) { return false; } @Override public boolean containsAll(Collection c) { return false; } @Override public Object[] toArray(Object[] a) { return new Object[0]; } }
1.反射之4種new對(duì)象
public class Test2 { public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException { //第一種 Phone p = new Phone(2999,"小米"); System.out.println(p);//Phone{price=2999.0, name='小米'} //第二種 需要一個(gè)空參構(gòu)造 Class<Phone> phoneClass = Phone.class; Phone phone = phoneClass.newInstance(); phone.setName("華為"); phone.setPrice(3499); System.out.println(phone);//Phone{price=3499.0, name='華為'} //第三種 Class<?> aclass = Class.forName("com.demo.bean.Phone"); Phone p2 = (Phone) aclass.newInstance(); p2.setPrice(2999); p2.setName("魅族"); System.out.println(p2);//Phone{price=2999.0, name='魅族'} //第四種,需要一個(gè)配置文件phone.properties String name = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> bClass = Class.forName(name); Phone p3 = (Phone) bClass.newInstance(); p3.setPrice(3299); p3.setName("錘子"); System.out.println(p3);//Phone{price=3299.0, name='錘子'} } }
配置文件phone.properties
myphone=com.demo.bean.Phone
2. 反射之獲取類、父類、實(shí)現(xiàn)接口
public class Test3 { public static void main(String[] args) throws ClassNotFoundException { String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //獲取類的完整路徑 System.out.println(aClass.getName());//com.demo.bean.Phone //獲取類的簡(jiǎn)單名字 System.out.println(aClass.getSimpleName());//Phone //獲取類的父類 Class<?> superclass = aClass.getSuperclass(); System.out.println(superclass.getName());//java.lang.Object System.out.println(superclass.getSimpleName());//Object //獲得類的接口 Class<?>[] interfaces = aClass.getInterfaces(); for (Class<?> in:interfaces ) { System.out.println(in.getSimpleName()); } } }
3.反射之獲取空參、有參構(gòu)造
public class Test4 { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException { String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //調(diào)用的是無參的構(gòu)造方法 Phone p1 = (Phone) aClass.newInstance(); p1.setName("華為"); p1.setPrice(2999);//Phone{price=2999.0, name='華為'} System.out.println(p1); //獲得無參的構(gòu)造方法 Constructor<?> constructor = aClass.getConstructor(); System.out.println(constructor);//public com.demo.bean.Phone() //獲得所有的構(gòu)造方法 Constructor<?>[] constructors = aClass.getConstructors(); for (Constructor<?> c:constructors ) { System.out.println(c); } } }
4.反射之獲取方法
public class Test5 { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException,InstantiationException,IllegalAccessException,InvocationTargetException{ String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //包含了父類的方法 Method[] methods = aClass.getMethods(); for (Method m:methods ) { System.out.println(m); } //本類中的方法,沒有父類的方法 Method[] declaredMethods = aClass.getDeclaredMethods(); for (Method m:declaredMethods ) { System.out.println(m); } Method gege = aClass.getMethod("gege",String.class); //獲取gege方法的權(quán)限修飾符 System.out.println(Modifier.toString(gege.getModifiers())); //獲取gege方法的返回值類型 System.out.println(gege.getReturnType()); //設(shè)置gege的參數(shù)值 Object o = aClass.newInstance(); gege.invoke(o,"aa"); } }
5.反射之獲取字段
public class Test6 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException { String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //只能調(diào)用public 字段,但是能得到父類的字段 Field[] fields = aClass.getFields(); for (Field f:fields ) { System.out.println(f.getName()); } //只能調(diào)用public 字段,只能得到本類中的字段 Field[] declaredFields = aClass.getDeclaredFields(); for (Field f:declaredFields ) { System.out.println(f.getName()); } //獲取某一字段的數(shù)據(jù)類型 Field name = aClass.getField("name"); String simpleName = name.getType().getSimpleName(); System.out.println(simpleName); name.setAccessible(true); Object o = aClass.newInstance(); name.set(o,"華為"); System.out.println(name.get(o)); } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié)
這篇文章主要介紹了SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Java中關(guān)于MouseWheelListener的鼠標(biāo)滾輪事件詳解
這篇文章主要介紹了Java中關(guān)于MouseWheelListener的鼠標(biāo)滾輪事件詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java多線程消息隊(duì)列的實(shí)現(xiàn)代碼
本篇文章主要介紹了java多線程消息隊(duì)列的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案
這篇文章主要介紹了SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案,文中通過代碼示例和圖文講解的非常詳細(xì),對(duì)大家解決問題有一定的幫助,需要的朋友可以參考下2024-05-05SpringBoot+微信小程序?qū)崿F(xiàn)文件上傳與下載功能詳解
這篇文章主要為大家介紹了SpringBoot整合微信小程序?qū)崿F(xiàn)文件上傳與下載功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-03-03MyBatis-Plus如何實(shí)現(xiàn)自動(dòng)加密解密
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)自動(dòng)加密解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Mysql存儲(chǔ)java對(duì)象實(shí)例詳解
這篇文章主要介紹了Mysql存儲(chǔ)java對(duì)象實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11自定義Spring Security的身份驗(yàn)證失敗處理方法
在本篇文章里小編給大家整理了一篇關(guān)于自定義Spring Security的身份驗(yàn)證失敗的處理方法,有需要的朋友們學(xué)習(xí)下。2019-05-05