我对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中的关键字?
发布评论