在 Java 中嵌套 类 并在 static main 中引用它们
nesting classes in Java and referencing them in static main
如果 class Animal 嵌套在 Test 中,我得到错误:
"non-static variable this cannot be referenced from a static context"
能否请您解释一下这个错误,并提供一种方法让这段代码正常工作,同时仍然保持嵌套 classes?我想学习使用嵌套 classes 并更好地理解它们。
a1创建在线时出现错误:Animal a1 = new Animal();
PS:当 Animal 是独立的 class(未嵌套)时,在测试之外 class,代码确实有效,但我对嵌套感兴趣 classes.
public class Test {
class Animal {
String colour;
void setColour(String x) {
colour = x;
}
String getColour() {
return colour;
}
}
public static void main(String[] args) {
Animal a1 = new Animal();
Animal a2 = a1;
a1.setColour("blue");
a2.setColour("green");
System.out.println(a1.colour);
System.out.println(a2.colour);
}
}
提前致谢。
要使其与嵌套的 classes 一起使用,请将 Animal
声明为 static
class:
public class Test {
static class Animal {
String colour;
void setColour(String x) {
colour = x;
}
String getColour() {
return colour;
}
}
public static void main(String[] args) {
Animal a1 = new Animal();
Animal a2 = a1;
a1.setColour("blue");
a2.setColour("green");
System.out.println(a1.colour);
System.out.println(a2.colour);
}
}
要了解为什么它以前不起作用,请阅读 Nested Classes documentation。关键大概是:
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
Animal
是 Test
的内部 class,这意味着它的任何实例都必须与封闭类型 Test
.[=17 的实例相关联=]
如果您希望将 Animal
用作常规 class 并使用 new Animal()
实例化它而不需要 Test
的实例,请将其更改为 static
并且您的主要方法将起作用。
正如其他人所说,您可以使用静态嵌套 class 从静态上下文中引用,或者通过 Test 实例实例化 Animal,如下所示:
new Test().new Animal();
您可以使 Animal class 静态并使代码工作。
否则,如果您不使内部 class(Animal) 静态化。内部 class(Animal) 的对象只能与外部 class(Test) 的实例一起存在。因此,如果您不想使其成为静态的,则必须先创建一个 Test 实例才能创建 Animal 实例。
例如
Animal a1 = new Test().new Animal();
参考 https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html 以更好地理解。
如果 class Animal 嵌套在 Test 中,我得到错误:
"non-static variable this cannot be referenced from a static context"
能否请您解释一下这个错误,并提供一种方法让这段代码正常工作,同时仍然保持嵌套 classes?我想学习使用嵌套 classes 并更好地理解它们。
a1创建在线时出现错误:Animal a1 = new Animal();
PS:当 Animal 是独立的 class(未嵌套)时,在测试之外 class,代码确实有效,但我对嵌套感兴趣 classes.
public class Test {
class Animal {
String colour;
void setColour(String x) {
colour = x;
}
String getColour() {
return colour;
}
}
public static void main(String[] args) {
Animal a1 = new Animal();
Animal a2 = a1;
a1.setColour("blue");
a2.setColour("green");
System.out.println(a1.colour);
System.out.println(a2.colour);
}
}
提前致谢。
要使其与嵌套的 classes 一起使用,请将 Animal
声明为 static
class:
public class Test {
static class Animal {
String colour;
void setColour(String x) {
colour = x;
}
String getColour() {
return colour;
}
}
public static void main(String[] args) {
Animal a1 = new Animal();
Animal a2 = a1;
a1.setColour("blue");
a2.setColour("green");
System.out.println(a1.colour);
System.out.println(a2.colour);
}
}
要了解为什么它以前不起作用,请阅读 Nested Classes documentation。关键大概是:
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
Animal
是 Test
的内部 class,这意味着它的任何实例都必须与封闭类型 Test
.[=17 的实例相关联=]
如果您希望将 Animal
用作常规 class 并使用 new Animal()
实例化它而不需要 Test
的实例,请将其更改为 static
并且您的主要方法将起作用。
正如其他人所说,您可以使用静态嵌套 class 从静态上下文中引用,或者通过 Test 实例实例化 Animal,如下所示:
new Test().new Animal();
您可以使 Animal class 静态并使代码工作。
否则,如果您不使内部 class(Animal) 静态化。内部 class(Animal) 的对象只能与外部 class(Test) 的实例一起存在。因此,如果您不想使其成为静态的,则必须先创建一个 Test 实例才能创建 Animal 实例。
例如
Animal a1 = new Test().new Animal();
参考 https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html 以更好地理解。