zoukankan      html  css  js  c++  java
  • java反射基础应用备注

    反射机制应用很广泛。这里简单备注下

    package com.shone.ailin;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class Reflectdemo {
        
        public static void main(String[] args) throws Exception {
            
            
            printVar();
            
            createInstance();
    
            printAllVarName();
            
        }
        
        //打印所有成员变量名
        private static void printAllVarName() {
            // TODO Auto-generated method stub
            Point p = new Point(1,3);
            Field[] fields = p.getClass().getDeclaredFields();
            for(int i=0;i<fields.length;i++) {
                System.out.println("var["+i+"]="+fields[i].getName());
            }
            
        }
    
        //打印成员变量的值
        private static void printVar() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
            Point p = new Point(1,4);
            Field fX = p.getClass().getDeclaredField("x");
            fX.setAccessible(true); //这里必须要加上 。不然会抛出异常。因为是私有成员变量,所以,需要暴力的反射
            int x = (int)fX.get(p);
            System.out.println("x="+x);
            
        }
    
        //创建实例化对象并调用方法
        private static void createInstance() throws Exception, SecurityException {
            Constructor<Point> con = Point.class.getConstructor(int.class,int.class);
            try {
                Point instance = con.newInstance(1,3);
                
                Method m = instance.getClass().getDeclaredMethod("addCount", int.class,int.class);
                m.setAccessible(true);
                try {
                    int count = (int) m.invoke(instance,2,7);
                    System.out.println(count);
                    
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                
                
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
        
    
    }

    Point类

    package com.shone.ailin;
    
    public class Point {
        private int x ;
        public int y ;
        
        public Point(int x ,int y ) {
            // TODO Auto-generated constructor stub
            super();
            this.x = x;
            this.y = y;
        }
        
        private int addCount(int x,int y) {
            return x+y;
        }
    
    }
  • 相关阅读:
    linux添加超级用户
    MongDB配置方法
    【bzoj5174】[Jsoi2013]哈利波特与死亡圣器 二分+树形dp
    【bzoj3560】DZY Loves Math V 欧拉函数
    【bzoj5157】[Tjoi2014]上升子序列 树状数组
    【uoj#317】[NOI2017]游戏 2-SAT
    【bzoj5146】有趣的概率 微积分
    【bzoj4695】最假女选手 线段树区间最值操作
    【bzoj4355】Play with sequence 线段树区间最值操作
    【loj2319】[NOIP2017]列队 Splay(卡过)
  • 原文地址:https://www.cnblogs.com/shoneworn/p/8822885.html
Copyright © 2011-2022 走看看