Java ANTLR4:参考书示例静态类定义
在书中的许多例子中,特别是在第7章中,有一群树行者和监听器,它们是通过使用这个定义来定义的:Java ANTLR4:参考书示例静态类定义,java,Java,在书中的许多例子中,特别是在第7章中,有一群树行者和监听器,它们是通过使用这个定义来定义的: public static class PropertyFileLoader extends PropretyFileBaseListener{ ... public static class PropertyFileVisitor extends PropertyFileBaseVisitor<Void> ... 让侦听器运行的方法是编写一个静态main,它执行以下操作: public
public static class PropertyFileLoader extends PropretyFileBaseListener{
...
public static class PropertyFileVisitor extends PropertyFileBaseVisitor<Void>
...
让侦听器运行的方法是编写一个静态main,它执行以下操作:
public class MyListener extends CymbolBaseListener {
public void enterVarDecl(CymbolParser.VarDeclContext ctx)
{
System.out.println("I have found a variable declaration!");
}
}
public static void main(String[] args)
{
ANTLRFileStream input = new ANTLRFileStream(args[0]);
CymbolLexer mylexer = new CymbolLexer(input);
CommonTokenStream tokens = new CommonTokenStream(mylexer);
CymbolParser myparser = new CymbolParser(tokens);
MyListener listener = new MyListener();
ParseTreeWalker walker = new ParseTreeWalker();
ParseTree tree = myparser.rules();
walker.walk(listener, tree);
}
为什么作者让大多数示例使用static关键字:
public static class MyListener extends CymbolBaseListener
作为一个非静态类也可以使用。
我刚刚意识到书中的一些示例有static关键字,而其他示例没有,但我不明白其背后的意图是什么。是关于static关键字,还是为什么这些类的设计包含static属性?是的,为什么要用static属性设计这些类?当我使用antlr生成的访问者时,我通常不包括静态,所以你应该说它不是重复的。把这个提交给主持人。此外,没有代码也无法回答。我们需要更多的信息来理解设计决策。否则人们就会开始猜测。
public static class MyListener extends CymbolBaseListener