访问说明符和访问修饰符有什么区别?
在 Java 中,访问说明符和访问修饰符是同一件事吗?
In Java, are access specifiers and access modifiers the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 Java 中,访问说明符和访问修饰符是同一件事吗?
In Java, are access specifiers and access modifiers the same thing?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
“访问修饰符”是 Java 语言规范。 “访问说明符”在 Java API doc,但这是我第一次注意到这一点。最好还是坚持使用 JLS 术语。
"access modifier" is the official term for
private
,protected
andpublic
used in the Java language specification. "access specifier" is used synonymously in the Java API doc, but this is the first time I've noticed that. It's probably better to stick with the JLS term.C++ 程序员使用的术语访问说明符不在 java 中。在java中我们正式使用访问修饰符。
例如:当我们用 private, static 声明一个类时,编译器会清楚地显示错误消息,如下所示:
The term Access specifier used by c++ programmers not in java. In java Officially we use Access Modifier.
For example: when we declare a class with private, static the compiler clearly shows the error message as follows:
参考Sun Java文档,它们似乎是相同的:
访问说明符
上此页面。
Referring to the Sun Java Docs they both seem to be the same:
Access Modifier
access specifier
onthis page.
Java 基本上有 2 种类型的修饰符:
Java 访问修饰符和 Java 访问说明符是同一件事,分别是
public
、private
、受保护
。Java has basically 2 types of Modifiers:
Java access modifiers and Java access specifiers are the same thing, which are
public
,private
,protected
.在一些较旧的语言中,public、private、protected 和 default(如 C++)被视为访问说明符,其他所有内容都被视为访问修饰符,但在 Java 中没有说明符的术语,默认情况下所有内容都仅被视为修饰符。
所以 public、private、protected、default、final、abstract、static、strictfp、synchronized、native、transient 和 volatile 都只是修饰符。
简单的测试是当我们编译以下代码时
private class Test{
我们
将得到编译时错误,指出此处不允许使用修饰符 private。对于其他修饰符也是如此。
也许java编译器(javac)只将所有内容视为“修饰符”。
In some older languages public, private, protected and default like C++ are considered as access specifiers and everything else is considered as access modifier but in Java there is no terminology for specifier, everything is by default considered as modifier only.
So public, private, protected, default, final, abstract, static, strictfp, synchronized, native, transient and volatile are all modifiers only.
Simple test for it is when we compile the following code
private class Test{
}
we will get compile time error saying that modifier private not allowed here. This is true for other modifiers also.
Maybe java compiler (javac) sees everything as a "modifier" only.
在java中没有什么被称为“访问说明符”,在java中只有访问修饰符
这种误解来自像C++这样的语言,其中public, private、protected、default 被视为访问说明符,其余的(static、final 等)被视为访问修饰符
There is nothing known as "Access specifiers" in java, there are only Access modifiers in java
The misconception is from languages like C++ where public, private, protected, default are considered as Access specifiers and remaining (static, final, etc) are considered as access modifiers
在我看来,是的,这两个术语指的是同一件事,并且可以互换使用。
According to me, yes, both terms refer to the same thing and are used interchangeably.
JDI 参考是我在 Java 规范中唯一看到过“访问说明符”这个术语的地方。即使在那里,公共/受保护/私有/包也称为“修饰符”。在 Java 中确实没有理由使用术语“访问说明符”,这显然只是数千个页面中的一个错误。
That JDI reference is the only place I have ever seen the term 'access specifier' used in a Java specification. Even there, public/protected/private/package are also called 'modifiers'. There's really no reason to ever use the term 'access specifier' in Java, it is clearly just a mistake on one page out of many thousands.
从技术上讲,私有、公共、受保护和默认都被视为访问说明符。这些涉及谁可以...问题。据我所知,修饰符有 volatile、final、static、transient 等。这些修饰符涉及 how does .. 方面。
Technically speaking private, public, protected and default are treated as access specifiers. These deal with who can ... questions. The modifiers afaik are volatile, final, static, transient etc. These deal with how does .. aspect.