相当于java中的内部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20921365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:20:27  来源:igfitidea点击:

Equivalent of internal in java

javaaccess-modifiers

提问by dbw

What is equivalent of internalaccess modifier available in C# for method in Java?

internal在 C# 中为 Java 中的方法提供的访问修饰符等效于什么?

(I know defaulti.e. methods, variables without any scope are having packageaccess but I am looking for keyword equivalent)

(我知道defaultie 方法,没有任何范围的变量可以package访问,但我正在寻找等效的关键字)

How can we achieve methods with protected internalscope in Java?

我们如何protected internal在 Java 中实现具有作用域的方法?

采纳答案by Jon Skeet

There's no equivalent of an assembly in Java, so there can't be an equivalent of an access modifier which makes a member available within an assembly.

Java 中没有程序集的等价物,因此不能有访问修饰符的等价物,使成员在程序集中可用。

The closest you can get to internalis the default accessibility which is similar but based on package.

您可以获得的最接近的internal是默认可访问性,它类似但基于包。

The closest you can get to protected internalis protected(but again based on package). Note that protectedin Java alsogives access to the package automatically - there's nothing in Java which is as restrictive as C#'s protected(in terms of onlybeing available within subclasses).

最接近的protected internalprotected(但同样基于包)。请注意,protected在 Java 中可以自动访问包 - Java 中没有任何东西像 C# 那样具有限制性protected(就只能在子类中使用而言)。

From JLS 6.6.2(emphasis mine):

JLS 6.6.2(强调我的):

A protected member or constructor of an object may be accessed from outside the package in which it is declaredonly by code that is responsible for the implementation of that object.

对象的受保护成员或构造函数可以从包外部访问,在该包中仅由负责实现该对象的代码声明它

In other words, within the package in which it is declared, it's accessible to all code.

换句话说,在声明它的包中,所有代码都可以访问它。