您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java基礎之反射的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
前期準備
編寫一個真實類phone,實現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對象
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='小米'} //第二種 需要一個空參構造 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='魅族'} //第四種,需要一個配置文件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. 反射之獲取類、父類、實現接口
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 //獲取類的簡單名字 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.反射之獲取空參、有參構造
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); //調用的是無參的構造方法 Phone p1 = (Phone) aClass.newInstance(); p1.setName("華為"); p1.setPrice(2999);//Phone{price=2999.0, name='華為'} System.out.println(p1); //獲得無參的構造方法 Constructor<?> constructor = aClass.getConstructor(); System.out.println(constructor);//public com.demo.bean.Phone() //獲得所有的構造方法 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方法的權限修飾符 System.out.println(Modifier.toString(gege.getModifiers())); //獲取gege方法的返回值類型 System.out.println(gege.getReturnType()); //設置gege的參數值 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); //只能調用public 字段,但是能得到父類的字段 Field[] fields = aClass.getFields(); for (Field f:fields ) { System.out.println(f.getName()); } //只能調用public 字段,只能得到本類中的字段 Field[] declaredFields = aClass.getDeclaredFields(); for (Field f:declaredFields ) { System.out.println(f.getName()); } //獲取某一字段的數據類型 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)); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java基礎之反射的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。