• 反射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();
            } 
            
        }
        
        
        
    
    }
  • 相关阅读:
    P1308 统计单词数(cin,getline() ,transform() )
    解决ASP.NET中的各种乱码问题
    GUID
    c# Thread、ThreadPool、Task的区别
    线程学习参考
    异步
    Lamda简单使用
    ubuntu上安装docker
    Git设置ssh密钥
    Git客户端(TortoiseGit)基本使用详解
  • 原文地址:https://www.cnblogs.com/lxh520/p/9082386.html
Copyright © 2020-2023  润新知