访问说明符和访问修饰符有什么区别?

发布于 2024-08-20 22:52:56 字数 52 浏览 8 评论 0原文

在 Java 中,访问说明符访问修饰符是同一件事吗?

In Java, are access specifiers and access modifiers the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

青柠芒果 2024-08-27 22:52:56

“访问修饰符”是 Java 语言规范。 “访问说明符”在 Java API doc,但这是我第一次注意到这一点。最好还是坚持使用 JLS 术语。

"access modifier" is the official term for private, protected and public 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.

↙温凉少女 2024-08-27 22:52:56

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:
enter image description here

猫性小仙女 2024-08-27 22:52:56

参考Sun Java文档,它们似乎是相同的:

Referring to the Sun Java Docs they both seem to be the same:

虫児飞 2024-08-27 22:52:56

Java 基本上有 2 种类型的修饰符:

  1. java 访问修饰符
  2. java 非访问修饰符

Java 访问修饰符和 Java 访问说明符是同一件事,分别是 publicprivate受保护

Java has basically 2 types of Modifiers:

  1. java access modifiers
  2. java non-access modifiers

Java access modifiers and Java access specifiers are the same thing, which are public, private, protected.

聆听风音 2024-08-27 22:52:56

在一些较旧的语言中,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.

无力看清 2024-08-27 22:52:56

在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

Following is the proof as compiler says "modifier private not allowed
here" i.e. compiler said modifier and not specifier

enter image description here

瀟灑尐姊 2024-08-27 22:52:56

在我看来,是的,这两个术语指的是同一件事,并且可以互换使用。

According to me, yes, both terms refer to the same thing and are used interchangeably.

野侃 2024-08-27 22:52:56

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.

孤独难免 2024-08-27 22:52:56

从技术上讲,私有、公共、受保护和默认都被视为访问说明符。这些涉及谁可以...问题。据我所知,修饰符有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文