反射 Reflection
来源:互联网 发布:淘宝二手电器 编辑:程序博客网 时间:2024/05/21 15:15
下面是一个简单反射类的应用
学生类
public class Student { private Student(){ } public Student(String stuid){ this.stuid = stuid; } private String stuid; public String getStuid() { return stuid; } public void setStuid(String stuid) { this.stuid = stuid; } private void getPicName(String pic){ System.out.println(pic); } @Override public String toString() { return "Student [stuid=" + stuid + "]"; }}
反射调用
public class Realize { public static void main(String[] args) { try { //get public Constructor Class<?> c = Class.forName("com.test.ref.Student"); Constructor<?> constructor = c.getConstructor(String.class); Object obj = constructor.newInstance("this is my test example"); System.out.println(obj.toString()); //get private Constructor Constructor<?> co = c.getDeclaredConstructor(); //private constructor need to access co.setAccessible(true); obj = co.newInstance(); System.out.println(obj.toString()); //get method Method method = obj.getClass().getDeclaredMethod("getPicName", String.class); method.setAccessible(true); //get all method Method[] methods = obj.getClass().getDeclaredMethods(); for (Method mm : methods) { System.out.println("method name : " + mm.getName()); } //get the parameters of the specified method Class<?>[] paramClasses = method.getParameterTypes() ; for (Class<?> class1 : paramClasses) { System.out.println("parameter type: " + class1.getName()); } // whether it is a private method System.out.println(method.getName() + " is private " + Modifier.isPrivate(method.getModifiers())); //call method method.invoke(obj, "Let me think"); // get all field Field[] publicFields = obj.getClass().getDeclaredFields(); for (Field field : publicFields) { System.out.println("field name : " + field.getName()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
0 0
- 反射(Reflection)
- 反射reflection
- 反射Reflection
- Reflection反射
- 反射 reflection
- 反射Reflection
- Reflection反射
- 反射reflection
- 反射(Reflection)
- Reflection 反射
- 反射(Reflection)
- 反射 Reflection
- 反射:reflection
- Java Reflection (JAVA反射)
- Java Reflection (JAVA反射)
- Java Reflection (JAVA反射)
- Java Reflection (JAVA反射)
- Java Reflection (JAVA反射)
- IP地址分类和子网划分
- linux:输入输出重定向
- 认识XML
- 更换移动硬盘显示图标
- js 时间格式
- 反射 Reflection
- oracle 的错误整理
- 416.。。课后总结
- git使用方法教程
- VM356:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
- OpenGLES的简单应用绘图,以及照相机画面变色变形渲染(二)
- spark on yarn 两种运行模式(client 、cluster)对比
- 超图报错解决
- chkconfig