【问题标题】:java: anonymous class implements interface; cannot have arguments [duplicate]java:匿名类实现接口;不能有参数[重复]
【发布时间】:2016-01-19 10:46:28
【问题描述】:

我的以下代码出错了!!!在 spark.get(new Route("/"). 我是新来的火花任何帮助将不胜感激??任何建议

public static void main(String[] args) {
        final Configuration configuration=new Configuration();
        configuration.setClassForTemplateLoading(sparkclass.class,"/");
        final StringWriter writer=new StringWriter();
        Spark.get(new Route("/"){
            public Object handle(Request request, Response response) throws Exception {
                Template helloTemplate=configuration.getTemplate("hello.ftl");

                HashMap<String,Object>mp=new HashMap<String, Object>();
                mp.put("name","Sumit");
                helloTemplate.process(mp,writer);
                System.out.print(writer);
                return writer;
            }
        });
    }

【问题讨论】:

  • 请贴出错误,否则很难猜到。
  • java:匿名类实现接口;不能有参数这是第号行的错误。 5
  • Spark.get("/", new Route() {...} 应该可以代替
  • "java:匿名类实现接口;不能有参数",编译器再明显不过了。实现接口的匿名类如何具有接受参数的构造函数

标签: java maven freemarker


【解决方案1】:

匿名类不能有显式声明的构造函数。 相反,Java 编译器必须自动提供一个匿名的 匿名类的构造函数。

因此你不能传递 arg。

参考:http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.5.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多