您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java基礎(chǔ)之反射的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
前期準(zhǔn)備
編寫(xiě)一個(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)用的是無(wú)參的構(gòu)造方法 Phone p1 = (Phone) aClass.newInstance(); p1.setName("華為"); p1.setPrice(2999);//Phone{price=2999.0, name='華為'} System.out.println(p1); //獲得無(wú)參的構(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); } //本類中的方法,沒(méi)有父類的方法 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)); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java基礎(chǔ)之反射的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。