创建内部类的新实例的Java语法的起源?

时间:2022-06-05 00:37:29

I'm curious as to the syntax choice for instantiating an inner class given an instance of the outer class in Java.

我对在Java中实例化内部类的语法选择很感兴趣。

The syntax is:

的语法是:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

But why is it not:

但为什么不是:

OuterClass.InnerClass innerObject = new outerObject.InnerClass(); 

The former seems to imply that new is a method or operator directly associated with the class, but my understanding is that this is not the case (unlike C++)?

前者似乎暗示了new是与类直接关联的方法或操作符,但我的理解是这不是事实(不像c++)?

3 个解决方案

#1


9  

The latter would imply to me that the type name was outerObject.InnerClass - whereas actually the typename is just InnerClass (or OuterClass.InnerClass, which would also be legal) constructed with relation to the instance referred to by outerObject.

后者将向我暗示类型名是outerObject。内类——而实际上,typename只是内类(或外类)。内部类,它也将是合法的)与由outerObject所引用的实例建立关系。

Personally I don't like the way Java does nested classes in the first place, and I agree it looks a little bit odd, but I can see why it's done that way.

就我个人而言,我不喜欢Java一开始嵌套类的方式,我同意它看起来有点奇怪,但我能理解为什么它是这样做的。

#2


3  

There's another reason as well.

还有另外一个原因。

new o.C(); 

has two possible meanings. It could mean the creation of an inner class C in the context of an outer object o. It could also be the creation of an class whose fully qualified name is o.C. This ambiguity is problematic, especially since both interpretations could be valid at the same time.

有两个可能的含义。它可能意味着在外部对象o的上下文中创建一个内部类C,也可能是创建一个完全限定名为o.C的类。这种模糊性是有问题的,特别是因为这两种解释同时都是有效的。

#3


1  

The inner class is a subset of, in all ways an intrinsic part of the outer class. The inner class can access all of the outer class' members, as if they were one. Similarly, an instance of a (non-static) inner class must be an intrinsic part of an instance of the outer class. It cannot exist on its own. You could compare it to how you can't access a non-static variable or function with MyClass.wyVariable.

内部类是外部类的固有部分的子集。内部类可以访问所有外部类的成员,就好像它们是一个。类似地,一个(非静态)内部类的实例必须是外部类实例的固有部分。它不可能独立存在。您可以将它与无法使用MyClass.wyVariable访问非静态变量或函数进行比较。

This raises the point of which way is the most semantically correct way of representing this relationship.

这就提出了用哪种方式来表达这种关系,在语义上是最正确的。

OuterClass.InnerClass innerObject = new outerObject.InnerClass();

... would (semantically) assert that InnerClass is somehow a property of outerObject which, once fetched, can be handled freely out in the world outside the scope of outerObject. new in this case would be nothing special.

…将(语义上)断言,InnerClass以某种方式是outerObject的属性,它一旦获取,就可以在outerObject范围之外的世界中*地处理。在这种情况下,新的做法没什么特别的。

Rather InnerClass is a part of OuterClass whereas an instance of InnerClass must a part of an instance of OuterClass.

内类是外类的一部分,而内类的实例必须是外类实例的一部分。

The syntax above would symbolize that InnerClass (the class itself) is a part of some instance of OuterClass, which is semantically incorrect. There is after all only one class InnerClass, even if this class may have any number of instances.

上面的语法将表示InnerClass(类本身)是某个OuterClass实例的一部分,这在语义上是不正确的。毕竟只有一个类InnerClass,即使这个类可能有很多实例。

OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

... would of course also be (semantically) incorrect for non-static inner classes, as it in no way represents the special loving and caring relationship between the instances of the inner and outer class.

…当然,对于非静态的内部类,当然也会(语义上)不正确,因为它绝不代表内部和外部类的实例之间的特殊的爱和关心关系。

Note however, that this syntax is perfectly fine for static inner classes, as they have no special relationship to any one instance of the outer class. It's also perfectly fine when instantiating an inner class inside its outer class.

但是,请注意,这种语法对于静态内部类是完全合适的,因为它们与外部类的任何一个实例都没有特殊的关系。当在它的外部类中实例化一个内部类时,它也非常好。

Finally,

最后,

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

... symbolizes the relationship better. It symbolizes that the process of instantiation is uniquely tied to a single, specific outer object, which is the case.

…象征着更好的关系。它标志着实例化过程是唯一地与一个特定的外部对象绑定在一起的,这是事实。

#1


9  

The latter would imply to me that the type name was outerObject.InnerClass - whereas actually the typename is just InnerClass (or OuterClass.InnerClass, which would also be legal) constructed with relation to the instance referred to by outerObject.

后者将向我暗示类型名是outerObject。内类——而实际上,typename只是内类(或外类)。内部类,它也将是合法的)与由outerObject所引用的实例建立关系。

Personally I don't like the way Java does nested classes in the first place, and I agree it looks a little bit odd, but I can see why it's done that way.

就我个人而言,我不喜欢Java一开始嵌套类的方式,我同意它看起来有点奇怪,但我能理解为什么它是这样做的。

#2


3  

There's another reason as well.

还有另外一个原因。

new o.C(); 

has two possible meanings. It could mean the creation of an inner class C in the context of an outer object o. It could also be the creation of an class whose fully qualified name is o.C. This ambiguity is problematic, especially since both interpretations could be valid at the same time.

有两个可能的含义。它可能意味着在外部对象o的上下文中创建一个内部类C,也可能是创建一个完全限定名为o.C的类。这种模糊性是有问题的,特别是因为这两种解释同时都是有效的。

#3


1  

The inner class is a subset of, in all ways an intrinsic part of the outer class. The inner class can access all of the outer class' members, as if they were one. Similarly, an instance of a (non-static) inner class must be an intrinsic part of an instance of the outer class. It cannot exist on its own. You could compare it to how you can't access a non-static variable or function with MyClass.wyVariable.

内部类是外部类的固有部分的子集。内部类可以访问所有外部类的成员,就好像它们是一个。类似地,一个(非静态)内部类的实例必须是外部类实例的固有部分。它不可能独立存在。您可以将它与无法使用MyClass.wyVariable访问非静态变量或函数进行比较。

This raises the point of which way is the most semantically correct way of representing this relationship.

这就提出了用哪种方式来表达这种关系,在语义上是最正确的。

OuterClass.InnerClass innerObject = new outerObject.InnerClass();

... would (semantically) assert that InnerClass is somehow a property of outerObject which, once fetched, can be handled freely out in the world outside the scope of outerObject. new in this case would be nothing special.

…将(语义上)断言,InnerClass以某种方式是outerObject的属性,它一旦获取,就可以在outerObject范围之外的世界中*地处理。在这种情况下,新的做法没什么特别的。

Rather InnerClass is a part of OuterClass whereas an instance of InnerClass must a part of an instance of OuterClass.

内类是外类的一部分,而内类的实例必须是外类实例的一部分。

The syntax above would symbolize that InnerClass (the class itself) is a part of some instance of OuterClass, which is semantically incorrect. There is after all only one class InnerClass, even if this class may have any number of instances.

上面的语法将表示InnerClass(类本身)是某个OuterClass实例的一部分,这在语义上是不正确的。毕竟只有一个类InnerClass,即使这个类可能有很多实例。

OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

... would of course also be (semantically) incorrect for non-static inner classes, as it in no way represents the special loving and caring relationship between the instances of the inner and outer class.

…当然,对于非静态的内部类,当然也会(语义上)不正确,因为它绝不代表内部和外部类的实例之间的特殊的爱和关心关系。

Note however, that this syntax is perfectly fine for static inner classes, as they have no special relationship to any one instance of the outer class. It's also perfectly fine when instantiating an inner class inside its outer class.

但是,请注意,这种语法对于静态内部类是完全合适的,因为它们与外部类的任何一个实例都没有特殊的关系。当在它的外部类中实例化一个内部类时,它也非常好。

Finally,

最后,

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

... symbolizes the relationship better. It symbolizes that the process of instantiation is uniquely tied to a single, specific outer object, which is the case.

…象征着更好的关系。它标志着实例化过程是唯一地与一个特定的外部对象绑定在一起的,这是事实。