使用与枚举的接口 [英] Using interface with enums

查看:114
本文介绍了使用与枚举的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个关于医疗信息的小型项目,在其中,我正在从几个数据源读取文本。所有这些文本来源具有相同的类型信息,但标签稍微不同。例如,sourceA有一个名为不利影响的部分,而sourceB将其称为副作用。



我有一个接口叫做 Reader ,以及实现此接口的几个类( AReader BReader 等)。此外,我有每个类的枚举为节标题。例如:

 枚举亚文{
SIDE_EFFECTS(副作用),
DOSAGE );
private String sectionTitle;
private ASections(String s){this.sectionTitle = s;
}

enum BSections {
SIDE_EFFECTS(不利影响),
DOSAGE(剂量和使用);
private String sectionTitle;
private BSections(String s){this.sectionTitle = s; }
}

在我的项目的核心是一个 Orchestrator 类,它使用读者(实际的来源A,B等由命令行选项指定)。



但是,我希望实现 Reader 的所有类也实现一个方法 getSectionText ,其中的参数应该是ASections或BSections或...



如何在界面级别指定这样的方法?



这是我第一次(显然是错误的)尝试:

  public String getSectionText(Enum _section_enum); 

这个想法是,无论在命令行中指定了哪个数据源,我都应该能够

解决方案

创建一个界面来定义方法你的枚举必须实现:

 接口部分{
String getSectionTitle();
}

在您的枚举中,实现该界面:

 枚举亚区实现部分{
SIDE_EFFECTS(副作用),
DOSAGE(剂量);
private String sectionTitle;
private ASections(String s){this.sectionTitle = s; }
public String getSectionTitle(){return sectionTitle;
}

枚举BSections实现部分{
SIDE_EFFECTS(不利影响),
DOSAGE(剂量和使用);
private String sectionTitle;
private BSections(String s){this.sectionTitle = s; }
public String getSectionTitle(){return sectionTitle; }
}

在您的阅读器中, getSectionText 方法采用 Sections 参数,而不是任何特定的枚举(代码对接口而不是实现):

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

然后你可以通过一个 ASections BSections 到读者中:

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

输出:

副作用
剂量
不利影响
剂量和用量

顺便说一句,问题中定义的枚举有错误。构造函数是public的,但是在Java中,枚举的构造函数必须是私有的。


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.

解决方案

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.

这篇关于使用与枚举的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆