开发者

Naming public static final in lowercase?

I have a hierarchy of classes:

class Page {
}
class ChildPage1 extends Page {
    private static final String URL;
}
class ChildPage2 extends Page {
    private final String url;
}

Some pages have static urls but other don't. I need some methods like load() in parent class Page that will use url of it's descendant. I thought about implementing it in these ways:

  • using reflection api
  • naming static final URLs in lowercase (in conroversary to na开发者_运维百科ming conventions).

Is it worth to not follow naming conventions in this case?


You can solve your problem by adding an abstract method getUrl() to Page that has to be implemented in ChildPage1 and ChildPage2.

class Page {
    protected abstract String getUrl();
}

class ChildPage1 extends Page {
    private static final String URL;

    protected String getUrl() {
        return URL;
    }
}

class ChildPage2 extends Page {
    private final String url;

    protected String getUrl() {
        return url;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜