java中反射简单使用示例
这篇文章将为大家详细讲解有关java 中反射简单使用示例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
专注于为中小企业提供成都网站制作、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业遂宁免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
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); } } -------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------- 测试类: 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(); } //调用非私有成员函数 @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); } //调用私有成员函数 @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); } //获取非私有构造函数 @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()); } //获取私有构造函数 @Test public void getPrivateConstructor() throws Exception { //该出方法与非私有构造器区别getDeclaredConstructor Constructor> construct = clazz.getDeclaredConstructor(String.class); //获得权限 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); } }
关于“java 中反射简单使用示例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
当前标题:java中反射简单使用示例
分享路径:http://myzitong.com/article/poisjp.html