简体   繁体   English

类如何在Java中实例化?

[英]How do classes instantiate in Java?

I came accross the following rule in JLS8/15.9.1 : 我在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 . 新标记之后的标识符必须明确表示可访问的内部类,非抽象的,不是枚举类型,以及主表达式或 ExpressionName 的编译时类型的 成员

I can't imagine what it the last restriction means. 我无法想象最后一次限制意味着什么。 Maybe you can give an example of such a member of the compile-time type of the Primary expression of the ExpressionName ? 也许您可以举例说明ExpressionNamePrimary表达式的编译时类型的成员?

It says "If the class instance creation expression is qualified" .. then .. (your quotation) 它说“如果类实例创建表达式是合格的”..那么..(你的报价)

So, I guess it's this case: 所以,我猜是这种情况:

package test;

public class Test1 {
    public class Test3{

    }
}

And you instantiate it in another class like this: 并在另一个类中实例化它,如下所示:

package test;

import test.Test1.Test3;

public class Maker {

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

}

Then, 然后,

  1. The instance creation expression is qualified: test1.new Test3() (test1.new, - a qualified new and not an unqualified new) 实例创建表达式是限定的: test1.new Test3() (test1.new, - 一个合格的new而不是一个不合格的new)
  2. The primary expression is test1 主要表达式是test1
  3. The compile-time type of the primary expression is Test1 主表达式的编译时类型是Test1
  4. The Identifier after the new token is Test3 and it unambiguously denotes the class Test3 新标记之后的标识符是Test3,它明确表示类Test3
  5. Test3 is accessible, non-abstract, not an enum type, and is a member of the compile-time type of Test1, the compile-time type of the primary expression. Test3是可访问的,非抽象的,不是枚举类型,并且是Test1的编译时类型的成员,Test1是主表达式的编译时类型。

Enjoy :) 请享用 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM