Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 枚举与布尔类的列表_Java - Fatal编程技术网

Java 枚举与布尔类的列表

Java 枚举与布尔类的列表,java,Java,现在,我有一个带有字段的类 @Entity public class Fuel { @Id @GeneratedValue private Long id; private boolean diesel; private boolean gasoline; private boolean etanhol; private boolean cng; private boolean electric; public Fuel()

现在,我有一个带有字段的类

@Entity
public class Fuel {

    @Id @GeneratedValue
    private Long id;

    private boolean diesel;
    private boolean gasoline;
    private boolean etanhol;
    private boolean cng;
    private boolean electric;

    public Fuel() {
        // this form used by Hibernate
    }

    public List<String> getDeclaredFields() {
        List<String> fieldList = new ArrayList<String>();

        for(Field field : Fuel.class.getDeclaredFields()){
            if(!field.getName().contains("_") && !field.getName().equals("id") && !field.getName().equals("serialVersionUID") ) {
                fieldList.add(field.getName());

            }
            Collections.sort(fieldList);
        }
        return fieldList;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean isDiesel() {
        return diesel;
    }

    public void setDiesel(boolean diesel) {
        this.diesel = diesel;
    }

    public boolean isGasoline() {
        return gasoline;
    }

    public void setGasoline(boolean gasoline) {
        this.gasoline = gasoline;
    }

    public boolean isEtanhol() {
        return etanhol;
    }

    public void setEtanhol(boolean etanhol) {
        this.etanhol = etanhol;
    }

    public boolean isCng() {
        return cng;
    }

    public void setCng(boolean cng) {
        this.cng = cng;
    }

    public boolean isElectric() {
        return electric;
    }

    public void setElectric(boolean electric) {
        this.electric = electric;
    }   

}
但是,由于市场上存在混合动力车(如丰田普锐斯),父类将以这种方式实现布尔类:

public enum Fuel {
    DIESEL("diesel"),
    GASOLINE("gasoline"),
    ETANHOL("etanhol"),
    CNG("cng"),
    ELECTRIC("electric");

    private String label;

    private Fuel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

}
private Fuel fuel = new Fuel();
private List<Fuel> fuelList = new ArrayList<Fuel>();
如果以这种方式使用枚举列表:

public enum Fuel {
    DIESEL("diesel"),
    GASOLINE("gasoline"),
    ETANHOL("etanhol"),
    CNG("cng"),
    ELECTRIC("electric");

    private String label;

    private Fuel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

}
private Fuel fuel = new Fuel();
private List<Fuel> fuelList = new ArrayList<Fuel>();
private-List-fuelist=new-ArrayList();
最佳做法是什么?请记住,我可能有100种不同的燃料(例如=)。不要忘记它是一个实体,因此会持久化到数据库中


提前谢谢=)

我觉得你想要一个枚举集,是的,肯定是一堆布尔

这让我想起了很多旗帜的设计模式,我最近发布了一个关于这一点的问题:


这可以轻松支持100种不同的燃料类型。然而,它并不支持同时使用100种不同燃料类型的汽车。但对我来说,这听起来很好——制造这样一辆车是非常困难的,这完全反映在编码的程序复杂性上:)(当然,除非它真的只是支持所有基于玉米的燃料——你可能更喜欢多态模式。)

你应该明确使用枚举

要获取对象的燃料类型的图像

如果你使用bools,你会得到这样的结果:

if (myClass.IsGasoline())
else if (myClass.IsOtherFuel())
else if
...
如果使用枚举,您只需执行以下操作:

Fuel fuel = myClass.GetFuelType()

(这只是伪代码;)

如果混合的数量很低,我想最好使用enum,并将混合作为另一种情况。 否则,您将不得不以一种可能很麻烦的方式管理逻辑,因为当您将某个燃料设置为true时,您很可能还必须将当前燃料设置为true。我这样说是因为你们有燃料类别的设定者,你们不仅仅是在建筑上定义


编辑:关于如何询问您正在使用的燃料类型的方法也将是支持enums的一个论据。

为什么EnumSet不支持同时使用100种燃料?您只需调用EnumSet.allOf(Fuel.class),它将提供一个包含每种燃油类型的EnumSet。@user1515834,当添加新的燃油类型时,它将中断,但汽车没有重新设计。您好,谢谢您的回答。然而,我认为对于具有多个选择器的对象,我选择布尔类,对于具有单个选择器的对象,我选择枚举类。看看:谷歌“过早优化”,然后试着发布一个“哪个更快?”的问题,看看你会得到什么样的回复。