什么时候在java中包装私有(没有显式修饰符)? [英] When to package-private (no explicit modifier) in java?

查看:147
本文介绍了什么时候在java中包装私有(没有显式修饰符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读教程控制对班级成员的访问。我很困惑使用package-private可能是一个很好的用例。因为据我所知,您可以随时将包声明更改为此类的包声明,并将其视为公共类。我明白这不是一件好事,但是什么阻止了我?

I have been reading the tutorial Controlling Access to Members of a Class. I am confused what might be good use case for using package-private. Because as I understand, you can always change your package declaration to whatever the package declaration of such a class and act as if that is a public class. I understand that this is not a good thing to do, but what is stopping me?

推荐答案


因为根据我的理解,您可以随时将包声明更改为此类的包声明,并将其视为公共类

Because as I understand, you can always change your package declaration to whatever the package declaration of such a class and act as if that is a public class

嗯,首先,访问修饰符是 help 开发人员。总有办法解决这些问题,例如通过反思。 / p>

Well, for one thing, the access modifiers are there to help the developer. There's always ways around them, such as via reflection for instance.


我知道这不是一件好事,但是什么阻止了我?

I understand that this is not a good thing to do, but what is stopping me?

真的不多!

作为开发人员,你可以然而,在密封的.jar文件中分发您的课程,这基本上意味着您不会让任何其他人进入您的包裹。

As a developer you can however distribute your classes in sealed .jar-files which basically means that you're not letting anyone else in to your packages.

来自< a href =http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html =nofollow noreferrer>封装JAR文件中的包


在JAR文件中封装软件包

JAR文件中的软件包可以是可选的sealed,表示该包中定义的所有类必须存档在同一个JAR文件中。例如,您可能希望密封包,以确保软件中类之间的版本一致性。

Packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.

这篇关于什么时候在java中包装私有(没有显式修饰符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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