zoukankan      html  css  js  c++  java
  • reflect

    package one;
    
    public class A {
        private int age;
    
        public A(int age) {
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class B {
    
        public static void main(String[] args) {
            A a = new A(10);
            Class clazz = a.getClass();
    
            try {
                System.out.println(a.getAge());
                Method setMethod = clazz.getDeclaredMethod("setAge", new Class[] { int.class });
                setMethod.invoke(a, new Object[] { Integer.valueOf(999) });
                System.out.println(a.getAge());
    
                Method getMethod = clazz.getDeclaredMethod("getAge", new Class[] {});
                Integer age = (Integer) getMethod.invoke(a, new Object[] {});
                System.out.println("---" + age.intValue());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }    
    }
  • 相关阅读:
    C++扬帆远航——4(百钱百鸡)
    C++扬帆远航——3(打印图形)
    C++扬帆远航——2
    web开发之Servlet 三
    web开发之Servlet 二
    web开发之Servlet 一
    迟来的2017年计划
    JSP 学习二
    JSP 学习一
    window7 32位安装Oracle11g
  • 原文地址:https://www.cnblogs.com/muhe221/p/7895184.html
Copyright © 2011-2022 走看看