package com.luoluo.cc;
public class meReflect {
@SuppressWarnings("unused")
}
}
// 获取指定的方法,调用re类的私有方法;
try {
m = re.getClass().getDeclaredMethod("t",int.class);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class meReflect {
@SuppressWarnings("unused")
private void t(int n){
System.out.println("类的私有方法");}
private static void tt(){
System.out.println("类的静态私有方法");}
}
利用反射机制
package com.luoluo.testCc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.luoluo.cc.meReflect;
public class testMeReflect {
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
meReflect re = new meReflect();
Method m;
//Class cls = re.getClass();// 获取指定的方法,调用re类的私有方法;
try {
m = re.getClass().getDeclaredMethod("t",int.class);
m.setAccessible(true);//压制java的访问修饰符;
m.invoke(re, 1);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}