java - 匿名类声明中的语句

标签 java anonymous-class

我正在阅读 oracle 文档中的匿名类(class)教程 (https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)

我已经复制了教程中使用的代码。(评论Statement1和Statement2是我附加的):

public class HelloWorldAnonymousClasses {

    interface HelloWorld {
        public void greet();
        public void greetSomeone(String someone);
    }

    public void sayHello() {

        class EnglishGreeting implements HelloWorld {
            String name = "world";
            public void greet() {
                greetSomeone("world");
            }
            public void greetSomeone(String someone) {
                name = someone;
                System.out.println("Hello " + name);
            }
        }

        HelloWorld englishGreeting = new EnglishGreeting();

        HelloWorld frenchGreeting = new HelloWorld() {
            String name = "tout le monde"; //Statement1
            public void greet() {
                greetSomeone("tout le monde");
            }
            public void greetSomeone(String someone) {
                name = someone;
                System.out.println("Salut " + name);
            }
        };

        HelloWorld spanishGreeting = new HelloWorld() {
            String name = "mundo"; //Statement2
            public void greet() {
                greetSomeone("mundo");
            }
            public void greetSomeone(String someone) {
                name = someone;
                System.out.println("Hola, " + name);
            }
        };
        englishGreeting.greet();
        frenchGreeting.greetSomeone("Fred");
        spanishGreeting.greet();
    }

    public static void main(String[] args) {
        HelloWorldAnonymousClasses myApp =
            new HelloWorldAnonymousClasses();
        myApp.sayHello();
    }            
}

教程继续解释: 匿名类表达式由以下部分组成:

  1. 新的运算符

  2. 要实现的接口(interface)或要扩展的类的名称。在此示例中,匿名类正在实现 HelloWorld 接口(interface)。

  3. 包含构造函数参数的圆括号,就像普通的类实例创建表达式一样。注意:当您实现一个接口(interface)时,没有构造函数,因此您使用一对空括号,如本例所示。

  4. 一个主体,它是一个类声明主体。 更具体地说,在正文中,方法声明是允许的,但语句是不允许的。

我对第 1 点感到困惑。 4 以上。它说匿名类声明主体中不允许使用语句,但我可以看到其中使用的语句。 (我已经添加了注释 Statement1 和 Statement2 以突出显示它们)。

您能否从第 1 点解释本教程想要传达的内容? 4?

提前致谢。

最佳答案

Statements不允许意味着不允许以下任何一项

  • 赋值表达式(aValue = 8933.234;)
  • 任何++ 或 -- (aValue++;) 的使用
  • 方法调用 (System.out.println("Hello World!");)
  • 对象创建表达式(Bicycle myBike = new Bicycle();)

但是this文档还说,

Note that you can declare the following in anonymous classes:

  • Fields
  • Extra methods (even if they do not implement any methods of the supertype)
  • Instance initializers
  • Local classes
    HelloWorld spanishGreeting = new HelloWorld() {

        String name = "mundo"; //Statement2

        double aValue = 0.0;

        String s = "Hi"; //instance variable initializers are allowed

       // assignment statement
        aValue = 8933.234; // not allowed

        // increment statement
        aValue++; // not allowed

        // method invocation statement
        System.out.println("Hello World!"); // not allowed

        // object creation statement
        Bicycle myBike = new Bicycle(); //instance variable initializers are allowed

        public void greet() {
            greetSomeone("mundo");
        }
        public void greetSomeone(String someone) {
            name = someone;
            System.out.println("Hola, " + name);
        }
    };

希望这对您有所帮助。

关于java - 匿名类声明中的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34333866/

相关文章:

java - ListView 仅显示 ListView 第一个位置的最后一项

java - 从匿名类到 lambda 表达式

java - 如何检查 Iterator.next() 是否为 == null?

c# - 如何扩展匿名类的对象

Java 反射与抽象类

带有参数的Java接口(interface)作为另一个接口(interface)

java - Java 循环双向链表 - 说明

java - 为什么此代码对 final 修饰符产生 false?

java - GWT 中的 MVP : Best design practice

java - 从程序文件获取文件