简体   繁体   English

Java枚举在实践中,不良代码得以改善

[英]Java enum in practice, bad code to improve

I want to improve my use of JDK 1.5 and stop using private static final String instead of enum. 我想改善对JDK 1.5的使用,并停止使用私有静态最终String而不是枚举。 This is what seems to be recommended. 这似乎是值得推荐的。

But now my constant class looks like this : 但是现在我的常量类看起来像这样:

public class CEnum{
    /**
     * @author JJA
     * date : 20/10/2010
     */
    public enum ListTypeAffichage {
        DEP("DEPOT_TVA"), PAD("PAS_DEPOT_TVA"), NORM("CAT_NORMALE"), CAP("CAT_PARTICULIERE"), CAA("CAT_AUTRE_CAS");

        private final String sName;

        /**
         * @param name String
         */    
        ListTypeAffichage(String name) {
            this.sName = name;
        }

        /**
         * @return String
         */
        public String getType() {
            return sName;
        }        
    }

    /**
     * @author JJA
     * date : 20/10/2010
     */
    public enum ListTypeCategorie {
        DEDUIRE("SOMME_A_DEDUIRE"), AJOUTER("SOMME_A_AJOUTER");

        private final String sName;

        /**
         * @param name String
         */
        ListTypeCategorie(String name) {
            this.sName = name;
        }

        /**
         * @return String
         */
        public String getType() {
            return sName;
        }    
    }

    /**
     * @author JJA
     * date : 26/10/2010
     */
    public enum ListInterval {
        POS("POSITIF"), NS("NON_SIGNE");

        private final String sName;

        /**
         * @param name String
         */
        ListInterval(String name) {
            this.sName = name;
        }

        /**
         * @return String
         */
        public String getInterval() {
            return sName;
        }    
    }
}

instead of 代替

public class ConstantesADMD {
    public static final List<String> typeAffich = new ArrayList<String>();
...
    ConstantesADMD(){
        typeAffich.add("DEPOT_TVA");
        typeAffich.add("PAS_DEPOT_TVA");
        typeAffich.add("CAT_NORMALE"); 

...       
    }
}

My code seems to be really bad, but at least works. 我的代码似乎很糟糕,但至少可以正常工作。 For each enum I have to add the redundant code : 对于每个枚举,我必须添加冗余代码:

private final String sName;

/**
 * @param name String
 */    
ListTypeAffichage(String name) {
    this.sName = name;
}

/**
 * @return String
 */
public String getType() {
    return sName;
}

What improvment do you advise me? 您对我有什么建议? Note : forget the last sentences of my first question, I need the index. 注意:忘记我第一个问题的最后一句话,我需要索引。 Tell me if I have to post another question, editing my fisrt seems easier. 告诉我是否需要发布其他问题,编辑我的第一件事情似乎更容易。

I would name my enum-constants as you have named your strings. 我将用您的字符串命名来命名我的枚举常量。 You can then access the name using the Enum.toString() method. 然后,您可以使用Enum.toString()方法访问该名称。 For example: 例如:

public enum ListTypeAffichage {
    DEPOT_TVA, PAS_DEPOT_TVA, CAT_NORMALE, CAT_PARTICULIERE, CAT_AUTRE_CAS;

    /**
     * @return String
     */
    public String getType() {
        return toString();
    }        
}

You could of course also skip the getType() all together, and access the "type" using toString() instead: 当然,您也可以一起跳过getType() ,而使用toString()来访问“类型”:

ListTypeAffichage myEnum = ListTypeAffichage.CAT_PARTICULIRE;
System.out.println("Type: " + myEnum.toString());             // like this...
System.out.println("Type: " + myEnum);                        // ...or like this

According to the API, this is better than using the Enum.name() directly: 根据API,这比直接使用Enum.name()更好:

public final String name()
[...] Most programmers should use the toString() method in preference to this one [...] [...] 大多数程序员应该优先使用toString()方法,而不是这一方法

Each enum has a name() method which return the exact string represantation of the constant. 每个枚举都有一个name()方法,该方法返回常量的确切字符串表示形式。 So you may do this: 因此,您可以这样做:

 public enum ListTypeAffichage {
        DEPOT_TVA, PAS_DEPOT_TVA, CAT_NORMALE, CAT_PARTICULIERE, CAT_AUTRE_CAS                  
    }

and then 接着

ListTypeAffichage.DEPOT_TVA.name();

By using the abbreviations (DEP, PAD, NORM) etc. you have created aliases to (DEPOT_TVA, PAS_DEPOT_TVA, CAT_NORMALE) etc. 通过使用缩写(DEP,PAD,NORM)等,您已经创建了(DEPOT_TVA,PAS_DEPOT_TVA,CAT_NORMALE)等的别名。

If you want to keep the abbreviations, then you'll have to maintain the enum as you have it. 如果要保留缩写,则必须保持枚举不变。

If you are willing to do away with the abbreviations, then you can change you enum as below, I have included a main method in the enum to demonstrate its use. 如果您愿意省略缩写,则可以按如下所示更改枚举,我在枚举中包含了一个主要方法来演示其用法。

public enum ListTypeAffichageNames {
    DEPOT_TVA,
    PAS_DEPOT_TVA,
    CAT_NORMALE,
    CAT_PARTICULIERE,
    CAT_AUTRE_CAS;


    public static void main(String[] args) {
        System.out.println(DEPOT_TVA.toString());
        ListTypeAffichageNames affichage = ListTypeAffichageNames.valueOf("DEPOT_TVA");
        System.out.println(affichage.toString());
    }
}  

In addition to this, your current structure of constants does not give you compile time type checking, and does not prevent something like the following happening during runtime: 除此之外,您当前的常量结构不会给您进行编译时类型检查,也不会阻止在运行时发生以下情况:

    ConstantesADMD.typeAffich.clear();
    // or
    ConstantesADMD.typeAffich.remove("DEPOT_TVA");
    ConstantesADMD.typeAffich.add("dEpOt-tVa");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM