Using interface with enums

China☆狼群 提交于 2019-12-11 06:56:30

问题


I am coding a mini-project of sorts about medical information, and in it, I am reading text from several data sources. All these text sources have the same type of information, but under slightly different labels. For example, sourceA has a section titled "adverse effects" while sourceB calls it "side effects".

I have an interface called Reader, and several classes (AReader, BReader, etc.) implementing this interface. Also, I have enum for each class for the section titles. For example:

enum ASections {
    SIDE_EFFECTS ("side effects"),
    DOSAGE       ("dosage");
    private String sectionTitle;
    private ASections(String s) { this.sectionTitle = s; }
}

enum BSections {
    SIDE_EFFECTS ("adverse effects"),
    DOSAGE       ("dosage and usage");
    private String sectionTitle;
    private BSections(String s) { this.sectionTitle = s; }
}

At the core of my project lies an Orchestrator class, which uses a Reader (the actual source A, B, etc. are specified by command line options). So far so good.

However, I want all the classes implementing Reader to also implement a method getSectionText, where the argument should be ASections or BSections or ...

How do I specify such a method at the interface level?

This was my first (obviously wrong) attempt:

public String getSectionText(Enum _section_enum);

The idea is that no matter which data source is specified at command line, I should be able to get the required type of text by fetching the appropriate section title.


回答1:


Create an interface that defines the method your enums must implement:

interface Sections {
    String getSectionTitle();
}

In your enums, implement that interface:

enum ASections implements Sections {
    SIDE_EFFECTS ("side effects"),
    DOSAGE       ("dosage");
    private String sectionTitle;
    private ASections(String s) { this.sectionTitle = s; }
    public String getSectionTitle() { return sectionTitle; }
}

enum BSections implements Sections {
    SIDE_EFFECTS ("adverse effects"),
    DOSAGE       ("dosage and usage");
    private String sectionTitle;
    private BSections(String s) { this.sectionTitle = s; }
    public String getSectionTitle() { return sectionTitle; }
}

In your Reader, the getSectionText method takes a Sections parameter instead of any specific enum (code against the interface, not the implementation):

class Reader {
    public String getSectionText(Sections sections) {
        return sections.getSectionTitle();
    }
}

Then you can pass either an ASections or a BSections into the reader:

public class Section8 {
    public static void main(String[] args) {
        Reader reader = new Reader();
        for (ASections asection : ASections.values()) {
            System.out.println(reader.getSectionText(asection));
        }
        for (BSections bsection : BSections.values()) {
            System.out.println(reader.getSectionText(bsection));
        }
    }
}

Output:

side effects
dosage
adverse effects
dosage and usage

By the way, there is an error in your enums as defined in the question. The constructor is public, however in Java the constructor of an enum must be private.




回答2:


just have the enum implement an interface, MySection, which specifies one method of getSectionText.



来源:https://stackoverflow.com/questions/21787711/using-interface-with-enums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!