Java语言规范 - 无法理解'BlockStatement' [英] Java Language Specification - Cannot understand 'BlockStatement'

查看:1097
本文介绍了Java语言规范 - 无法理解'BlockStatement'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Java语言规范这里(相反,我应该喝啤酒)我很好奇方法可以包含什么。规范声明方法体可以包含块

I've been examining the Java Language Specification here (instead I should be out having a beer) and I am curious about what a method can contain. The specification states a method body can contain a block

MethodBody:
    Block

'Block'包含'BlockStatements'。 'BlockStatement'规则如下所示:

Where a 'Block' contains 'BlockStatements'. The 'BlockStatement' rule looks like this:

BlockStatement : 
    LocalVariableDeclarationStatement
    ClassOrInterfaceDeclaration
    [Identifier :] Statement

我可以理解'LocalVariableDeclarationStatement',它可能是

I can understand the 'LocalVariableDeclarationStatement' which could be

[final] int x, y, z;

但是,我没有 为什么 那里有'ClassOrInterfaceDeclaration'规则。此规则如下所示:

However, I don't get why the 'ClassOrInterfaceDeclaration' rule is there. This rule looks like:

ClassOrInterfaceDeclaration: 
    ModifiersOpt (ClassDeclaration | InterfaceDeclaration)

ClassDeclaration: 
    class Identifier [extends Type] [implements TypeList] ClassBody

InterfaceDeclaration: 
    interface Identifier [extends TypeList] InterfaceBody

这里发生了什么 - 你不能肯定地在一个区块内声明一个类或接口?
有人可以帮助解释这种混乱吗?

What's going on here - You can't declare a class or interface within a block surely? Can someone help elucidate this confusion please?

更新:我可以在方法中定义一个类,但以下方法不起作用:

Update: I can define a class within a method, but the following won't work:

public class Foo {
    public void doFoo() {
        interface dooJa {
            int bar();
        }
    }
}

编译器抱怨说The成员接口dooJa只能在顶级类或接口中定义...任何解释?

The compiler complains stating "The member interface dooJa can only be defined inside a top-level class or interface"... any explanations?

推荐答案

你已经很好地观察了不再工作的接口。原因是你正在寻找一个非常古老的语法版本。它看起来已经超过10岁了。看看Java 6的语法(您可能正在测试的内容):

You've made a good observation about interfaces not working anymore. The reason is you that are looking at a very old version of the grammar. It looks to be over 10 year old. Take a look the grammar for Java 6 (what you are probably testing with):

http://www.it.bton.ac.uk/staff/rnb/bosware/javaSyntax/rulesLinked.html#BlockStatement

您将看到封闭声明:


BlockStatement:
LocalVariableDeclarationStatement
ClassDeclaration
Statement

BlockStatement: LocalVariableDeclarationStatement ClassDeclaration Statement

这篇关于Java语言规范 - 无法理解'BlockStatement'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆