zoukankan      html  css  js  c++  java
  • 反射

    package com.xxxx;
    
    import java.lang.reflect.Method;
    
    /**
     * 测试: 通过反射访问类私有成员
     * 
     * @author Dw
     *
     */
    public class AccessPrivateMethodWithReflect
    {
    	public static void main(String[] args)throws Exception
    	{
    		TestAccessPrivateMethod pm = new TestAccessPrivateMethod();
    		Class<?> classType = pm.getClass();
    		
    		// 获取TestAccessPrivateMethod类的privateMethod方法原型
    		Method method = classType.getDeclaredMethod("privateMethod", String.class);
    		
    		// 突破私有成员方法的private访问权限(只有getDeclaredMethod()方法返回的Method对象才能突破私有权限, getMethod()方法返回的Method对象则无此权限)
    		method.setAccessible(true);
    		
    		// 调用私有方法(privateMethod(string));
    		method.invoke(pm, "Hello World"); // Hello World
    
    	}
    }
    
    
    class TestAccessPrivateMethod
    {
    	// 私有方法
    	private void privateMethod(String str)
    	{
    		System.out.println(str);
    	}
    }
    

      

  • 相关阅读:
    HDOJ1024(最大M子段和)
    HDOJ1025(最长上升子序列)
    HDOJ1022(模拟栈)
    HDOJ(1018)
    HDOJ1238(string)
    HDOJ1015(简单深搜)
    HDOJ1016(标准dfs)
    Tabbar视图切换,返回上一视图,添加item
    页面转换方法
    网络状态判断
  • 原文地址:https://www.cnblogs.com/DuanLaoYe/p/7510051.html
Copyright © 2011-2022 走看看