类 如何在 Java 中实例化?

How do classes instantiate in Java?

我在 JLS8/15.9.1 中遇到了以下规则:

The Identifier after the new token must unambiguously denote an inner class that is accessible, non-abstract, not an enum type, and a member of the compile-time type of the Primary expression or the ExpressionName.

我无法想象最后一个限制是什么意思。也许你可以举一个 Primary 表达式 ExpressionName?

的编译时类型成员的例子

它说 "If the class instance creation expression is qualified" .. 然后 ..(你的报价)

所以,我猜是这样的:

package test;

public class Test1 {
    public class Test3{

    }
}

然后在另一个 class 中实例化它,如下所示:

package test;

import test.Test1.Test3;

public class Maker {

    public static void main(String[] args) {
    Test1 test1 = new Test1();
        Test3 test3 = test1.new Test3();    
    }

}

然后,

  1. 实例创建表达式合格: test1.new Test3() (test1.new, - 合格新品而非不合格新品)
  2. 主要表达式是 test1
  3. 主表达式的编译时类型是 Test1
  4. 新令牌后的标识符是 Test3,它明确表示 class Test3
  5. Test3 是可访问的、非抽象的、不是枚举类型,并且是 Test1 的编译时类型(主要表达式的编译时类型)的成员。

尽情享受吧:)