反射出类中的字段,构造函数,方法(私有的,静态的l诶)
来源:互联网 发布:冬季骑行服 知乎 编辑:程序博客网 时间:2024/05/06 20:12
反射技术:DEMO
实体类:
import java.io.InputStream;import java.util.List;import java.util.Map;/** * * @author allen * */public class Person {/** * 字段 */private String test1 = "allen";public static String test2 = "static";public String names = "fancy";public int test3 ;public double test4;public float test5;/** * 构造函数 */public Person(){System.out.println("无参构造函数");}public Person(int test3){this.test3 = test3;System.out.println("有一个参数的构造函数");}public Person(double test4,String names){this.test4 = test4;this.names = names;System.out.println("有俩个参数的构造函数");}public Person(List list){System.out.println("List");}private Person(Map map){System.out.println("private Map");}/** * 方法 */public void getNoParamMethod(){System.out.println("无参数传递进来");}public void getNoneParamMethod(String name){System.out.println("参数传递进来"+name);}public Class[] getParamMethod(String name,int[] arr){return new Class[]{String.class,int[].class};}private void getInputStream(InputStream inputStream){System.out.println("private inputStream :"+inputStream);}public static void getStaticMethod(int number){System.out.println("static method:"+number);}}
反射出构造函数:
import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.junit.Test;/** * * @author allen * */public class TestClazzConstrutor {/** * 反射出无参数的构造函数public Person() */@Testpublic void testNoParamConstructor(){Class clazz = Person.class;try {Constructor constructor = clazz.getConstructor(null);Person person = (Person) constructor.newInstance(null);System.out.println(person);} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 反射出public Person(int test3) */@Testpublic void testNoneParamConstructor(){Class clazz = Person.class;try {Constructor constructor = clazz.getConstructor(int.class);Person person = (Person) constructor.newInstance(12);System.out.println(person.test3);} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * public Person(double test4,String names) */@Testpublic void testTwoParamConstructor(){Class clazz = Person.class;try {Constructor constructor = clazz.getConstructor(double.class,String.class);Person person = (Person) constructor.newInstance(12,"allen");System.out.println(person.test4+person.names);} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * public Person(List list) */@Testpublic void testListParamConstructor(){Class clazz = Person.class;try {Constructor constructor = clazz.getConstructor(List.class);Person person = (Person) constructor.newInstance(new ArrayList());} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * private Person(Map map) */@Testpublic void testPrivateMapParamConstructor(){Class clazz = Person.class;try {Constructor constructor = clazz.getDeclaredConstructor(Map.class);//对私有的构造函数 constructor.setAccessible(true);Person person = (Person) constructor.newInstance(new HashMap());} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
反射出方法:
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.junit.Test;/** * * @author allen * */public class TestClazzMethod {/** * public void getNoParamMethod() */@Testpublic void testNoParamMethod(){Class clazz = Person.class;Person p = new Person();try {Method method = clazz.getDeclaredMethod("getNoParamMethod",null);method.invoke(p, null);} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic void testgetNoneParamMethod(){Class clazz = Person.class;Person p = new Person();try {Method method = clazz.getDeclaredMethod("getNoneParamMethod",String.class);method.invoke(p, "allen");} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * public Class[] getParamMethod(String name,int[] arr) */@Testpublic void testgetParamMethod(){Class clazz = Person.class;Person p = new Person();try {Method method = clazz.getDeclaredMethod("getParamMethod",String.class,int[].class);Class[] clazz1 = (Class[] )method.invoke(p, "allen",new int[]{1,2,3});System.out.println(clazz1.length);} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic void testgetInputStream() throws Exception{Class clazz = Person.class;Person p = new Person();try {Method method = clazz.getDeclaredMethod("getInputStream",InputStream.class);//私有方法,反射获取method.setAccessible(true);method.invoke(p,new FileInputStream(new File("D://redis.txt")));} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * */@Testpublic void testgetStaticMethod(){Class clazz = Person.class;Person p = new Person();try {Method method = clazz.getDeclaredMethod("getStaticMethod",int.class);method.invoke(p,12);} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}反射出字段:
import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import org.junit.Test;/** * * @author allen * */public class TestClazzField {/** * public * @throws Exception */@Testpublic void testField() throws Exception{Class clazz = Person.class;Person p = new Person();try {Field f = clazz.getField("names");Class type=f.getType();if(type.equals(String.class)){String names = (String)f.get(p);System.out.println(names);//设置值f.set(p, "xxxxxxxx");System.out.println(f.get(p));}} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * private * @throws Exception */@Testpublic void testPrivateField() throws Exception{Class clazz = Person.class;Person p = new Person();try {Field f = clazz.getDeclaredField("test1");f.setAccessible(true);Class type=f.getType();if(type.equals(String.class)){String names = (String)f.get(p);System.out.println(names);//设置值f.set(p, "xxxxxxxx");System.out.println(f.get(p));}} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * static * @throws Exception */@Testpublic void testStaticField() throws Exception{Class clazz = Person.class;Person p = new Person();try {Field f = clazz.getDeclaredField("test2");f.setAccessible(true);Class type=f.getType();if(type.equals(String.class)){String names = (String)f.get(p);System.out.println(names);//设置值f.set(p, "xxxxxxxx");System.out.println(f.get(p));}} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
0 0
- 反射出类中的字段,构造函数,方法(私有的,静态的l诶)
- java反射机制 以及通过反射获得类的变量字段,方法,构造函数
- java反射机制 以及通过反射获得类的变量字段,方法,构造函数
- 反射得到父类的私有字段
- 通过反射修改类的私有字段值,调用私有方法
- 通过反射修改类的私有字段值,调用私有方法
- 反射类的构造函数和方法
- 通过JAVA的反射调用类中的公有私有方法
- (104)反射:获取Class的对象、构造函数、字段、方法。反射实例练习
- 利用java反射调用类的的私有方法(包括父类中的私有方法)
- 私有的构造函数
- 反射破坏单例的私有构造函数保护
- Java利用反射访问私有构造函数的例子
- 反射调用私有实现类的方法出异常的分析
- JavaScript中的私有函数;Javascript构造函数的私有方法中访问其属性和公有方法
- Java反射——对类内部函数、私有变量和构造函数的使用举例
- Java通过反射来获取类的私有构造方法和成员方法
- 不包含继承时类中的静态块、静态方法、非静态方法、构造函数的执行顺序?
- Linux下软件安装方法总结:
- notepad++支持dwt,lbi格式并且高亮显示
- JAVA的StringBuffer类
- Freemark 学习笔记
- 线程的七种状态
- 反射出类中的字段,构造函数,方法(私有的,静态的l诶)
- 实际——如何给SQL查询添加合计行(sqlserver2008。但是db2的case when 语句end后不能添加其他字段)
- android HorizontalScrollView替代Gallery
- android从零开始--国际化、样式和主题、屏幕适配
- 基于FFmpeg的Android平台硬解播放器的移植(未完,待续)
- 用eclipse 单步调试php step by step
- 2014年工作中遇到的20个问题:181-200
- Ubuntu 使用apt-get install安装制定版本的mysql
- 搭建Windows Red5流媒体服务器详解