您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java 中反射簡(jiǎn)單使用示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
package reflect_test; public class Dog implements Animals { private int age =100; private String xstr="test..."; public int page = 100000; private Dog(String xstr) { this.xstr = xstr; } public Dog() { } public Dog(int age) { this.age = age; } @Override public String scrime(String str) { return str; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private int getAddAge() { return age+=10; } public void getXstr() { System.out.println(xstr); } } -------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------- 測(cè)試類: package reflect_test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.junit.Before; import org.junit.Test; public class TestDemo { Class<?> clazz= null; Class<?> interfa = null; Object obj = null; @Before public void init() throws ClassNotFoundException, InstantiationException, IllegalAccessException { clazz = Class.forName("reflect_test.Dog"); interfa = Class.forName("reflect_test.Animals"); obj = clazz.newInstance(); } //調(diào)用非私有成員函數(shù) @Test public void MethodTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method m = clazz.getMethod("scrime", String.class); Object object = m.invoke(obj, "say....."); System.out.println(object); } //調(diào)用私有成員函數(shù) @Test public void privateMethodTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method m = clazz.getDeclaredMethod("getAddAge"); m.setAccessible(true); Object object = m.invoke(obj); System.out.println(object); } //獲取非私有構(gòu)造函數(shù) @Test public void getNotPrivateConstructor() throws Exception { Constructor<?> construct = clazz.getConstructor(int.class); Dog dog = (Dog)construct.newInstance(10); System.out.println("The age is : "+dog.getAge()); } //獲取私有構(gòu)造函數(shù) @Test public void getPrivateConstructor() throws Exception { //該出方法與非私有構(gòu)造器區(qū)別getDeclaredConstructor Constructor<?> construct = clazz.getDeclaredConstructor(String.class); //獲得權(quán)限 construct.setAccessible(true); Dog dog = (Dog)construct.newInstance("1000"); dog.getXstr(); } //獲取非私有變量 @Test public void getField() throws Exception { Field f = clazz.getDeclaredField("age"); f.setAccessible(true); Object value = f.get(obj); System.out.println(value); } //獲取私有變量 @Test public void getPirvateField() throws Exception { Field f = clazz.getField("page"); Object value = f.get(obj); System.out.println(value); } }
關(guān)于“java 中反射簡(jiǎn)單使用示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(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)容。