“object.new”如何工作?(Java 有 .new 运算符吗?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2863157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:11:57  来源:igfitidea点击:

How does "object.new" work? (Does Java have a .new operator?)

javasyntaxinner-classes

提问by chickeninabiscuit

I came across this code today whilst reading Accelerated GWT (Gupta) - page 151.

我今天在阅读Accelerated GWT (Gupta) - page 151 时遇到了这段代码。

public static void getListOfBooks(String category, BookStore bookStore) {
    serviceInstance.getBooks(category, bookStore.new BookListUpdaterCallback());
}
public static void storeOrder(List books, String userName, BookStore bookStore) {
    serviceInstance.storeOrder(books, userName,    bookStore.new StoreOrderCallback());
}

What are those new operators doing there? I've never seen such syntax, can anyone explain?

那些新的运营商在那里做什么?我从未见过这样的语法,谁能解释一下?

Does anyone know where to find this in the java spec?

有谁知道在java规范中在哪里可以找到它?

采纳答案by cletus

They're inner(nested non-static) classes:

它们是内部(嵌套的非静态)类:

public class Outer {
  public class Inner { public void foo() { ... } }
}

You can do:

你可以做:

Outer outer = new Outer();
outer.new Inner().foo();

or simply:

或者干脆:

new Outer().new Inner().foo();

The reason for this is that Innerhas a reference to a specific instance of the outer class. Let me give you a more detailed example of this:

这样做的原因是它Inner引用了外部类的特定实例。让我给你一个更详细的例子:

public class Outer {
  private final String message;

  Outer(String message) {
    this.message = message;
  }

  public class Inner {
    private final String message;

    public Inner(String message) {
       this.message = message;
    }

    public void foo() {
      System.out.printf("%s %s%n", Outer.this.message, message);
    }
  }
}

and run:

并运行:

new Outer("Hello").new Inner("World").foo();

Outputs:

输出:

Hello World

Note:nested classes can be statictoo. If so, they have no implicit thisreference to the outer class:

注意:嵌套类也可以static。如果是这样,它们就没有this对外部类的隐式引用:

public class Outer {
  public static class Nested {
    public void foo() { System.out.println("Foo"); }
  }
}

new Outer.Nested.foo();

More often than not, static nested classes are privateas they tend to be implementation details and a neat way of encapsulating part of a problem without polluting the public namespace.

通常情况下,静态嵌套类private往往是实现细节和封装问题的一部分而不污染公共命名空间的巧妙方法。

回答by Greg Case

BookListUpdaterCallbackand StoreOrderCallbackare inner classes of BookStore.

BookListUpdaterCallback并且StoreOrderCallback是 BookStore 的内部类。

See The Java Tutorial - http://docs.oracle.com/javase/tutorial/java/javaOO/nested.htmland http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

请参阅 Java 教程 - http://docs.oracle.com/javase/tutorial/java/javaOO/nested.htmlhttp://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

回答by tangens

I haven't seen this syntax before either, but I think it will create an inner class of BookStore.

我之前也没有见过这种语法,但我认为它会创建一个BookStore.