在实例上调用 new,例如 pc.new InnerClass()——这是怎么回事?
Calling new on instance, like pc.new InnerClass() -- what is going on?
我对在实例上调用 new 的可能性感到困惑,比如
InnerClass sc = pc.new InnerClass();
我知道如何使用它,但我的问题是关于完全理解它。喜欢:
它在 JAVA 文档中的何处描述?
这是应该使用的推荐解决方案,还是有更好的方法?
为什么普通 "new" 不起作用?
我在代码示例中看到了它,并且我已经了解了我无法在静态上下文中使用纯 "new"。
这是作为可运行示例的完整上下文:
class ParentClass{
ParentClass(){
}
public static void main(String[] args){
ParentClass pc = new ParentClass();
InnerClass sc = pc.new InnerClass();
}
class InnerClass {
InnerClass() {
System.out.println("I'm OK");
}
}
}
免责声明:您在示例中使用的术语 "parent class" 和 "sub class" 不正确,因此我下面的示例将使用正确的术语 "outer class" 和 "inner class"(感谢@eis 的提示)。
Where is it described in the JAVA documentation?
请参阅@eis 对我的回答的评论 link。
Is this a recommended solution that should be used, or is there a better way?
这取决于 – 取决于您需要它做什么。
如果 SubClass
不需要 ParentClass
的 实例 的任何信息,它可以(并且应该)被设为静态或提取为不是内部 class 完全没有了。在这种情况下,您可以直接调用 new
而无需 ParentClass
.
的实例
Why doesn't a plain "new" work?
因为SubClass
可能会引用周围实例的信息,这需要你指定那个实例。它不是子class,因为它扩展了ParentClass
,而是它的类型变成了外部class.[=23 的成员 =]
考虑一下(并查看实际效果 here):
public class OuterClass {
private int field;
public OuterClass(int field) {
this.field = field;
}
class InnerClass {
public int getOuterClassField() {
// we can access the field from the surrounding type's instance!
return OuterClass.this.field;
}
}
public static void main(String[] args) throws Exception {
OuterClass parent = new OuterClass(42);
// prints '42'
System.out.println(parent.new InnerClass().getOuterClassField());
// cannot work as it makes no sense
// System.out.println(new InnerClass().getOuterClassField());
}
}
如果您能够简单地执行 new InnerClass()
,则无法知道 getOuterClassField
应该 return 什么,因为它连接到 实例 它周围的类型(而不仅仅是类型本身)。
我对在实例上调用 new 的可能性感到困惑,比如
InnerClass sc = pc.new InnerClass();
我知道如何使用它,但我的问题是关于完全理解它。喜欢:
它在 JAVA 文档中的何处描述?
这是应该使用的推荐解决方案,还是有更好的方法?
为什么普通 "new" 不起作用?
我在代码示例中看到了它,并且我已经了解了我无法在静态上下文中使用纯 "new"。
这是作为可运行示例的完整上下文:
class ParentClass{
ParentClass(){
}
public static void main(String[] args){
ParentClass pc = new ParentClass();
InnerClass sc = pc.new InnerClass();
}
class InnerClass {
InnerClass() {
System.out.println("I'm OK");
}
}
}
免责声明:您在示例中使用的术语 "parent class" 和 "sub class" 不正确,因此我下面的示例将使用正确的术语 "outer class" 和 "inner class"(感谢@eis 的提示)。
Where is it described in the JAVA documentation?
请参阅@eis 对我的回答的评论 link。
Is this a recommended solution that should be used, or is there a better way?
这取决于 – 取决于您需要它做什么。
如果 SubClass
不需要 ParentClass
的 实例 的任何信息,它可以(并且应该)被设为静态或提取为不是内部 class 完全没有了。在这种情况下,您可以直接调用 new
而无需 ParentClass
.
Why doesn't a plain "new" work?
因为SubClass
可能会引用周围实例的信息,这需要你指定那个实例。它不是子class,因为它扩展了ParentClass
,而是它的类型变成了外部class.[=23 的成员 =]
考虑一下(并查看实际效果 here):
public class OuterClass {
private int field;
public OuterClass(int field) {
this.field = field;
}
class InnerClass {
public int getOuterClassField() {
// we can access the field from the surrounding type's instance!
return OuterClass.this.field;
}
}
public static void main(String[] args) throws Exception {
OuterClass parent = new OuterClass(42);
// prints '42'
System.out.println(parent.new InnerClass().getOuterClassField());
// cannot work as it makes no sense
// System.out.println(new InnerClass().getOuterClassField());
}
}
如果您能够简单地执行 new InnerClass()
,则无法知道 getOuterClassField
应该 return 什么,因为它连接到 实例 它周围的类型(而不仅仅是类型本身)。