zoukankan      html  css  js  c++  java
  • 反射3

    package reflect.corecode;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    public class TestOne {
    
        public static void main(String[] args) {
            
            /**
             * 
             * 反射
             * 动态操纵Java代码,被大量应用于Javabean中。
             * 
             * getFields  当前类及其超类的公有域
             * getDeclaredFields 当前类的所有域
             * 
             * getMethods 当前类及其超类的公有方法
             * public Method[] getDeclaredMethods() throws SecurityException { 当前类的所有方法
             * getMethod(String arg0, Class... arg1) 获取当前类及超类的方法
             * 
             * 
             * Method类中invoke方法
             * invoke(Object arg0, Object... arg1)
             */
            
            ///////////////////////////////book
            System.out.println(Double[].class.getName());
            
            Man man = new Man();
            man.setAge(11); 
            Class class1 = man.getClass();
            Field[] field1 = class1.getFields();
            Field[] field2 = class1.getDeclaredFields();
            
            Method[] method1 = class1.getMethods();
            Method[] method2 = class1.getDeclaredMethods();
            try {
                Method method3 = class1.getMethod("getAge", null);  //get方法
                Method method4 = class1.getMethod("setAge", int.class); //set方法
                Object value = method3.invoke(man, null);
                System.out.println("----->>"+method3);
            } catch (Exception e) {
                e.printStackTrace();
            } 
            
        }
        
        
        
    
    }
  • 相关阅读:
    邮票面值设计(codevs 1047) 题解
    练习 : 生成器和模块
    练习 : 数据类型之字符串
    练习 : 函数基础
    练习 : 高阶函数
    练习 : 数据类型之列表
    练习 : 数据类型之元组
    练习 : 数据类型之字典
    练习 : 分支结构和循环结构
    练习 : 变量和运算符
  • 原文地址:https://www.cnblogs.com/lxh520/p/9082386.html
Copyright © 2011-2022 走看看