为什么Java缺少访问权限说明符? [英] Why Is Java Missing Access Specifiers?

查看:133
本文介绍了为什么Java缺少访问权限说明符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人理解为什么缺少Java:

Does anyone understand why Java is missing:


  • 一个访问说明符,允许类和所有子类访问,但不允许其他类访问同一个包中的类? (保护 - 减去)

  • 一个访问说明符,它允许类访问,同一个包中的所有类,以及任何子包中的所有类? (默认加)

  • 一个访问说明符,它将子包中的类添加到当前允许受保护访问的实体中? (保护加)

我希望我有更多的选择而不是受保护和默认。特别是,我对Protected-plus选项很感兴趣。

I wish I had more choices than protected and default. In particular, I'm interested in the Protected-plus option.

假设我想使用Builder / Factory图案类生成一个带有许多指向其他对象的链接的对象。对象上的构造函数都是默认的,因为我想强制您使用工厂类来生成实例,以确保正确完成链接。我想将工厂分组在一个子包中,以便将它们保持在一起并与它们实例化的对象区别开来 - 这对我来说似乎是一个更清洁的包结构。

Say I want to use a Builder/Factory patterned class to produce an object with many links to other objects. The constructors on the objects are all default, because I want to force you to use the factory class to produce instances, in order to make sure the linking is done correctly. I want to group the factories in a sub-package to keep them all together and distinct from the objects they are instantiating---this just seems like a cleaner package structure to me.

目前无法做到。我必须将构建器放在与它们构造的对象相同的包中,以获得对默认值的访问。但是从 project.area.objects.builders 中分离 project.area.objects 会非常好。

No can do, currently. I have to put the builders in the same package as the objects they are constructing, in order to gain the access to defaults. But separating project.area.objects from project.area.objects.builders would be so nice.

那么为什么Java缺少这些选项呢?并且,无论如何都要伪造它?

So why is Java lacking these options? And, is there anyway to fake it?

推荐答案

一次一个地回答你的问题:

To answer your questions one at a time:

* An access specifier which allows access by the class and all subclasses, but NOT by other classes in the same package? (Protected-minus)

这是在Java 1.0中(私有保护修饰符)并已删除。我不清楚为什么,但它肯定被视为不值得麻烦的东西。当前的安排使得所有修饰符从较少限制变为更受限制,这更简单(如在较小的潜在变化中),这是Java设计目标。

This was in Java 1.0 (private protected was the modifier) and removed. I'm not entirely clear on why, but it was definitely viewed as something not worth the hassle. The current arrangement makes all modifiers go from less restricted to more restricted which is simpler (as in less potential variation) which is a Java design goal.

* An access specifier which allows access by the class, all classes in the same package, AND all classes in any sub-package? (Default-plus)

* An access specifier which adds classes in sub-packages to the entities currently allowed access by protected? (Protected-plus)

这两项因相关原因无法使用。 Java目前没有子包的概念,它只是假装常规文件和基于jar的类加载器(以及编译器)遵循底层文件系统的目录结构来查找文件。 Java将使用JSR 294(超级包)更广泛地解决这个问题(正如其他人所指出的那样),这将使您对包外发布的内容进行更细粒度的控制(因此,如果您愿意,可以将内容公之于众)到,它仍然不可见。。

These two are not available for a related reason. Java currently doesn't have a notion of a sub-package, it just pretends to by the fact that the regular file and jar based classloaders (as well as the compiler) follow the directory structure of the underlying file system to find files. Java will be addressing this more extensively with JSR 294 (the "superpackage") (as others have pointed out) which will give you much more granular control of what is published outside of the package (and therefore you could make things public if you want to, and it still would not be visible).

这篇关于为什么Java缺少访问权限说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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