美文网首页
关于异常(包含部分暴力反射)

关于异常(包含部分暴力反射)

作者: 熨斗目花 | 来源:发表于2018-10-09 18:49 被阅读0次

    Demo:

    public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Test test = new Test(4,5);
    Field field1 = test.getClass().getDeclaredField("x");
    field1.setAccessible(true);
    System.out.println(field1.get(test)); }

    class Test {
    private int x;
    public int y;
    public Test(int x, int y) {
    super();
    this.x = x;
    this.y = y;
    }

    以上为暴力反射的一个例子。下面是需要抛出的异常(有提示)
    • NoSuchFieldException
    • IllegalAccessException

    重写的clone()需要抛出 CloneNotSupportedException

    相关文章

      网友评论

          本文标题:关于异常(包含部分暴力反射)

          本文链接:https://www.haomeiwen.com/subject/bluyaftx.html