把一个方法或者变量声明成private,一定安全么?求解释。
解决方案 »
- 关于类之间参数传递的问题 菜鸟请教大家
- 我想用java调用windows的命令提示符,请问可行吗?如果可以的话请告诉我从哪里入手开始写程序
- UltraEdit为什么列模式不好用啊
- 如何用JAVA获取两台机子之间的带宽呢?请教了!谢谢各位!
- 简单,白拿分(50分,弯个腰就有了)
- UI小问题,可我就是不知道怎么实现,或许您知道……
- 在Linux下安装JDK
- 解释一下PrintWriter和PrintStream类的作用和用法,谢谢
- 国内还有哪些公司在做java开发?
- 都说 thinking in java 中文版错误百出,希望诸位高人能一一举出,也好让我等拥有此书的菜鸟不至于误入歧途。
- 深度复制
- 在线等!servlet的问题,懂的人肯定觉得很简单!!
规则的制定不是为了让你违反的。
import java.lang.annotation.RetentionPolicy;@Retention(value = RetentionPolicy.RUNTIME)
public @interface Value
{
String[] value();
}public class Test01
{
//私有属性
private int a = 0; //私有方法
@Value(value = { "hello", "world" })
private int add(int b)
{
return a + b;
}
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;public class Test02
{
public static void main(String[] args) throws Exception
{
// Class cls = Test01.class; 或者
Test01 tst = new Test01();
Class cls = Class.forName("Test01"); // 反射设置属性
Field field = cls.getDeclaredField("a");
field.setAccessible(true); //更改为可访问
field.setInt(tst, 10); // 反射调用方法
Method method = cls.getDeclaredMethod("add", int.class);
method.setAccessible(true); //更改为可访问
int rst = (Integer) method.invoke(tst, 8);
System.out.println(rst); // 反射取得方法的注解
Annotation[] anns = method.getAnnotations();
for (Annotation a : anns)
{
if (a instanceof Value)
{
String[] strs = ((Value) a).value();
System.out.println(strs[0] + "$" + strs[1]);
}
} }
}