个人日记2017.08.12
来源:互联网 发布:淘宝信鸽出售 编辑:程序博客网 时间:2024/06/18 04:14
反射:
public class User {public String name;private int age;public User(String name, int age) {this.name = name;this.age = age;}public User() {}private void run() {System.out.println("我会跑");}private void run(String name) {System.out.println(name + "我会跑");}public void say() {System.out.println("我会说话");}public static void main(String[] args) throws ClassNotFoundException,NoSuchFieldException, SecurityException, NoSuchMethodException,IllegalAccessException, IllegalArgumentException,InvocationTargetException, InstantiationException {// 加载这个类的class文件Class c = Class.forName("com.sxt.User");// 创建一个实例化的对象User u = (User) c.newInstance();// 类的全路径System.out.println(c.getName());// 类的名称System.out.println(c.getSimpleName());// 返回类的权限System.out.println(Modifier.toString(c.getModifiers()));// 判断是否是接口System.out.println(c.isInterface());// 拿到父类的全称System.out.println(c.getSuperclass().getName());// 父类的名字System.out.println(c.getSuperclass().getSimpleName());// 获取类中的public的属性Field f = c.getField("name");System.out.println(f.getName());// 获取所有权限类型的属性Field age = c.getDeclaredField("age");System.out.println(age.getName());// 获取所有public的属性Field[] fs = c.getFields();// 获取所有类型的所有属性Field[] dfs = c.getDeclaredFields();System.out.println(dfs.length);System.out.println("======================================================");// 获取所有公共方法包含父类和接口Method[] ms = c.getMethods();for (Method m : ms) {System.out.println(m.getName());}System.out.println("======================================================");// 通过方法名获取公共方法Method m = c.getMethod("say");System.out.println(m.getName());System.out.println("======================================================");// 获取所有本类的方法Method[] bms = c.getDeclaredMethods();for (Method bm : bms) {System.out.println(bm.getName());}System.out.println("======================================================");// 通过名字获取本类的方法Method bm = c.getDeclaredMethod("run");System.out.println(bm.getName());// 调用本类的带参数的方法Method cm = c.getDeclaredMethod("run", String.class);cm.invoke(u, "张三");}}
阅读全文
0 0
- 个人日记2017.08.12
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 个人日记
- 【个人日记】
- 个人日记
- 个人日记
- [个人日记]心情好糟糕
- 个人日记:编程起步。
- Andrew Ng机器学习课程笔记--week7
- 谈谈spring定时调度的几种实现方式
- 机器学习之线性回归
- 多层json转xml
- 生成随机验证码
- 个人日记2017.08.12
- 解决“Some index files failed to download, they have been ignored, or old ones used instead.”
- Struts2入门(9):数据验校
- apache与php的关联
- C++之函数
- EasyDemo*android面试常见题
- 解决python写入csv后用wps打开乱码
- linux环境变量的修改操作
- hdu6108 小C的倍数问题(超简单题)