匿名 class 声明中的语句
Statements in anonymous class declaration
我正在阅读来自 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();
}
}
教程继续讲解:
匿名 class 表达式包含以下内容:
新运算符
要实现的接口名称或要扩展的 class。在此示例中,匿名 class 正在实现接口 HelloWorld.
包含构造函数参数的圆括号,就像正常的 class 实例创建表达式一样。注意:当你实现一个接口时,没有构造函数,所以你使用一对空括号,如本例所示。
A body,是一个class声明体。 更具体地说,在正文中,方法声明是允许的,但语句是不允许的。
我对第 1 点感到困惑。 4 以上。它说匿名 class 声明主体中不允许使用语句,但我可以看到其中使用的语句。
(我添加了注释 Statement1 和 Statement2 以突出显示它们)。
能否从第 1 点解释教程想要传达的内容? 4?
提前致谢。
您在 statement
和 field declaration
之间感到困惑。正如@sidgate 在评论中提到的,它不是语句而是实例定义和初始化。
为了更清楚地了解它
试着写一些像
这样的语句
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde"; //Statement1
System.out.print("this is a statement");// it wont compile
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
Statements are not allowed 表示 不允许以下任何一项.
- 赋值表达式(
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);
}
};
希望对您有所帮助。
好像晚了 3 年,但只是换一种理解方式。匿名 class 的 body 与任何其他顶级 class body.
一样
我们只能在 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();
}
}
教程继续讲解: 匿名 class 表达式包含以下内容:
新运算符
要实现的接口名称或要扩展的 class。在此示例中,匿名 class 正在实现接口 HelloWorld.
包含构造函数参数的圆括号,就像正常的 class 实例创建表达式一样。注意:当你实现一个接口时,没有构造函数,所以你使用一对空括号,如本例所示。
A body,是一个class声明体。 更具体地说,在正文中,方法声明是允许的,但语句是不允许的。
我对第 1 点感到困惑。 4 以上。它说匿名 class 声明主体中不允许使用语句,但我可以看到其中使用的语句。 (我添加了注释 Statement1 和 Statement2 以突出显示它们)。
能否从第 1 点解释教程想要传达的内容? 4?
提前致谢。
您在 statement
和 field declaration
之间感到困惑。正如@sidgate 在评论中提到的,它不是语句而是实例定义和初始化。
为了更清楚地了解它 试着写一些像
这样的语句HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde"; //Statement1
System.out.print("this is a statement");// it wont compile
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
Statements are not allowed 表示 不允许以下任何一项.
- 赋值表达式(
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);
}
};
希望对您有所帮助。
好像晚了 3 年,但只是换一种理解方式。匿名 class 的 body 与任何其他顶级 class body.
一样我们只能在 class 中有成员,即变量(有或没有初始化)和方法和初始化块。
任何语句只能出现在方法或初始化块中,但不能单独出现。