我可以创建自定义java.*包吗?

问题描述:

我可以自己创建一个与预定义软件包同名的软件包吗?
在Java中,例如java.lang?

Can I create a package of my own that has the same name as a predefined package
in Java, such as java.lang?

如果是这样,结果将是什么?这样不能让我访问该程序包的受保护的成员吗?

If so, what would the results be? Wouldn't that enable me to access that package's protected members?

如果没有,是什么阻止我这样做?

If not, what prevents me from doing so?

否-禁止java.lang.安全管理器不允许java.lang程序包中的自定义"类,也无法告诉他接受它们.

No - java.lang is prohibited. The security manager doesn't allow "custom" classes in the java.lang package and there is no way telling him to accept them.

您是对的-在java.lang命名空间中声明的自己的类将使您可以使用protected方法和该程序包中的类成员,而这绝对是不希望的.

You're right - own classes declared in the java.lang namespace would allow you to use protected methods and members of classes in that package, and this is definitly not wanted.

这可以很好地编译-但是-尝试执行它;)

This compiles fine - but - try to execute it ;)

package java.lang;

public class EvilAsEvilCanBe {

    public static void main(String[] args) {
        System.out.println("hehe");
    }

}