1

I'm trying to implement an abstract method in an enum as in code below, but Eclipse is underlining the .getUrls method and displaying the tooltip "this method requires a body instead of a semicolon, two quick fixes: (add body, add 'abstract' modifier)", none of which make sense to me.

The method itself is just a normal method that works in other classes.

What I'm doing wrong?

public enum Site {
    PORTAL1 ("http://www.somedomain.net") {
        @Override
        public void method() {
            ArrayList<String> urlList = urlManager.getUrls(int number);
        }
    };
    // more values

    public abstract void method();
    private final String baseUrl;
    private UrlManager urlManager;

    private Site(final String baseUrl) {
        this.baseUrl = baseUrl;
        urlManager = new UrlManager();
    }
}
4

4 回答 4

4

一个问题是以下行在语法上无效:

ArrayList<String> urlList = urlManager.getUrls(int number);
                                               ^^^^^^^^^^ HERE

另一个问题是您无法访问from的非static成员。您要么需要 make ,要么让每个子类都有自己的.SitePORTAL1UrlManager staticSiteUrlManager

于 2012-11-28T14:36:12.217 回答
2

除了明显的问题之外int number,您不能urlManager从枚举实例中引用。您可以对其进行保护,以便可以从每个实例访问它:

public enum Site {

    PORTAL1("http://www.somedomain.net") {
        @Override
        public void method() {
            ArrayList<String> urlList = urlManager.getUrls(5);
        }
    };
    // more values

    public abstract void method();
    private final String baseUrl;
    protected UrlManager urlManager;

    private Site(final String baseUrl) {
        this.baseUrl = baseUrl;
        urlManager = new UrlManager();
    }
}
于 2012-11-28T14:34:54.387 回答
0

由于枚举类未声明为抽象类,甚至可能不想被称为抽象类,请执行以下操作:

public void method() {
    }

非抽象类不能有抽象方法。

于 2012-11-28T14:32:57.350 回答
0

你有一些不正确的语法:

urlManager.getUrls(int number);

你是在声明一个变量,你应该在其中传递一个参数。另外,在哪里urlManager声明?

于 2012-11-28T14:37:53.287 回答