IllegalAccessError:访问受保护的方法 [英] IllegalAccessError: accessing a protected method

查看:131
本文介绍了IllegalAccessError:访问受保护的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级。

班级 A 有受保护的方法 m() a A 的实例。

Class A has protected method m(), a is an instance of A.

Class B 与类 A 在同一个包中。

Class B is in the same package as class A.

我正在尝试访问 am()但我得到 IllegalAccessError ...

I am trying to access a.m() but I am getting IllegalAccessError...

出了什么问题?

推荐答案

编译器应该捕获这样的错误。正如你在运行时显然得到的那样,发生了一些奇怪的事情。您可能已更改源代码但已完全重新编译。

The compiler should catch errors like this. As you are apparently getting this at runtime, something odd has happened. Probably you have changed the source code but fully recompiled.

另一个潜在但又模糊的问题是通过不同的类加载器加载类。从不同的类加载器加载的类将在不同的包中,即使包名相同(与不同类加载器加载的具有相同名称的类的方式相同)。

Another potential, but obscure, problem is loading the classes through different class loaders. Classes loaded from different class loaders will be in different packages even if the package name is the same (in the same way as classes with the same name loaded by different class loaders will be different classes).

这篇关于IllegalAccessError:访问受保护的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆