简体   繁体   English

什么是java新手?功能或关键字

[英]what is java new? A function or keyword

As i understand new is a keyword and not a function. 据我所知,new是关键字而不是函数。

For example 例如

A a = new A();

instantiates the object a of type A. 实例化A类型的对象a
A keyword is not associated with any object per se. 关键字本身与任何对象无关。

On the contrary, when we have in A a public inner class B we call 相反,当我们在A公共内部B级时,我们打电话

B b = a.new B()

Here it looks like new is a property of B and not an independent keyword. 这里看起来new是B的属性而不是一个独立的关键字。

What is the meaning of A.new ? A.new是什么意思?

New is a keyword in both cases. 在这两种情况下, New都是关键字。 It's part of a class instance creation expression . 它是类实例创建表达式的一部分。

There are two forms: unqualified and qualified . 有两种形式: 不合格合格

The unqualified form starts with the keyword 'new'. 不合格的表单以关键字“new”开头。

The qualified form starts with a primary class, then 'new'. 合格的表单以主要类开头,然后是“新”。 This allows creation of inner classes -- non-static nested classes that hold an implicit reference to an instance of the outer class. 这允许创建内部类 - 非静态嵌套类,其中包含对外部类的实例的隐式引用。 The qualified form provides a way to specify that instance. 限定表单提供了指定该实例的方法。

From the Java Language Specification, section 15.9 : Java语言规范,第15.9节

Unqualified class instance creation expressions begin with the keyword new. 非限定类实例创建表达式以关键字new开头。

An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top level (§7.6), member (§8.5, §9.5), local (§14.3) or anonymous class (§15.9.5). 无限制的类实例创建表达式可用于创建类的实例,无论该类是顶级(第7.6节),成员(第8.5节,第9.5节),本地(第14.3节)还是匿名类(第15.9.5)。

Qualified class instance creation expressions begin with a Primary. 合格的类实例创建表达式以Primary开头。

A qualified class instance creation expression enables the creation of instances of inner member classes and their anonymous subclasses. 限定类实例创建表达式允许创建内部成员类及其匿名子类的实例。

new is a keyword which has it's own syntax (as you have noticed). new是一个具有自己语法的关键字(正如您所注意到的)。 See JLS 3.9 JLS 3.9

Java doesn't have functions as such. Java没有这样的功能。 It has methods and Java 8 will add more functional features. 它有方法,Java 8将添加更多功能特性。

It would be B b = a.new B(); 它将是B b = a.new B(); and new is still just a keyword. 而new仍然只是一个关键字。 The reference to the object a shows the compiler that B is a nested class. 对象a的引用向编译器显示B是嵌套类。 http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

B b = A.new B(); // A should be an instance object not a class name,
                 // otherwise it's not a valid syntax

You are creating an object of type B that would have access to instance members of the instance A . 您正在创建一个类型B的对象,该对象可以访问实例A实例成员。

The new is for sure a key word in Java. 新的肯定是Java中的一个关键词。

This is part of new key word syntax. 这是新关键字语法的一部分。 That have two way of invocation, as you have presented. 正如您所呈现的那样,这有两种调用方式。 The second is used to instantiate the outer class first before you can instantiate inner class. 第二个用于在实例化内部类之前首先实例化外部类。

It true that might look like a property of class, but you will not be able to create such property in Java. 它确实看起来像是类的属性,但是你无法在Java中创建这样的属性。 As 'new' is key word so it can not be used as property. 因为“新”是关键词所以它不能用作财产。

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

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