什么是Java中的关键字?

编程入门 行业动态 更新时间:2024-10-25 22:24:08
本文介绍了什么是Java中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对Java中的关键字感到好奇.

I am curious about keywords in Java.

我应该把它理解为用用户定义类的每个实例(例如最终的超级"类)创建的各种方法的类吗?在Java中如何定义?

Should I understand it as a class of various methods that get created with every instance of user-defined classes, like the ultimate "super" class? How is it defined in the Java?

因此,例如,我遇到了这个问题:

So for example, I came across this:

class A { class B {} } A a = new A(); B b = a.new B();

似乎每个类都有关键字new作为其自己的方法.对于在Java中如何定义/实现关键字的任何见解,我将不胜感激.

This seems like each class has keyword new as its own method. I would appreciate any insights on how keywords are defined/implemented in Java.

推荐答案

类没有关键字"new"作为个人方法或类似方法. Java语言本身具有关键字"new".因此,换句话说,您将"new"放入代码中,编译器将识别出它并实例化一个新的Object.

Classes don't have keyword "new" as a personal method or anything like that. It is the Java language itself that has the keyword "new". So in other words you put "new" in the code the compiler would recognize it and instantiate a new Object.

docs .oracle/javase/specs/jls/se7/html/jls-3.html#jls-3.9- 此链接是Java语言的文档,在3.9节中显示了所有关键字.

docs.oracle/javase/specs/jls/se7/html/jls-3.html#jls-3.9- this link is the documentation of Java language, in section 3.9 it shows all the keywords.

就像其他人说的那样,问题中的代码片段表示内部类,例如,例如 docs.oracle/javase/tutorial/java/javaOO/nested.html

Like others are saying, what the snippet of code in your question indicates an inner class, so for instance, like it says in docs.oracle/javase/tutorial/java/javaOO/nested.html

public class ShadowTest { public int x = 0; class FirstLevel { public int x = 1; void methodInFirstLevel(int x) { System.out.println("x = " + x); System.out.println("this.x = " + this.x); System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); } } public static void main(String... args) { ShadowTest st = new ShadowTest(); ShadowTest.FirstLevel fl = st.new FirstLevel(); fl.methodInFirstLevel(23); } }

以下是此示例的输出:

x = 23 this.x = 1 ShadowTest.this.x = 0

这表明内部类或类B(FirstLevel)与类A(ShadowTest)的外部类的变量和方法(与外部类的实例相关联)相似或相似.

This shows that the innerclass or class B(FirstLevel) is like or similar to the outer class's variables and methods(for it is associated with the instance of the outer class) of class A(ShadowTest).

更多推荐

什么是Java中的关键字?

本文发布于:2023-06-08 21:24:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关键字   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!