如何定义常数值 - 最佳实践 [英] How to define Constant values - Best Practice

查看:151
本文介绍了如何定义常数值 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种方法来定义常量。第一个使用Enum将一组静态的最终DataType变量保存在一个类中。



这是第一个类型:

  public class TipTipProperties {
public static final String MAX_WIDTH_AUTO =auto;
public static final String POSITION_RIGHT =right;
}

这些变量的用法将通过静态调用,例如: code> TipTipProperties.MAX_WIDTH_AUTO



第二种类型是:

  public enum TipTipProperties {

MAX_WIDTH_AUTO(MaxWidth.AUTO),
POSITION_RIGHT(Position.RIGHT);

private MaxWidth maxWidth;
私人职位;

private TipTipProperties(MaxWidth maxWidth){
this.maxWidth = maxWidth;
}

private TipTipProperties(Position position){
this.position = position;
}

public MaxWidth getMaxWidth(){
return maxWidth;
}

public Position getPosition(){
return position;
}

public enum MaxWidth {
AUTO(auto);

private String width;

private MaxWidth(String width){
this.width = width;
}

public String getWidth(){
return width;
}
}

public enum位置{
RIGHT(right),

private String position;

private Position(String position){
this.position = position;
}

public String getPosition(){
return position;
}
}
}

作为示例用法: code> TipTipProperties.POSITION_RIGHT.getPosition()。getPosition()。



我的问题是:




  • 哪一个是更好的OOP,为什么?

  • 是否存在任何替代方案或更好的方法?



提前感谢

解决方案

枚举是最好的作为约书亚·布洛赫(Joshua Bloch)在有效的Java 中所说的,你将有更多的控制使用枚举,如你想打印所有的常数,可以。
与类常量不能有类型安全。阅读此内容以获取进一步帮助


I have two ways to define constants. First one holds the bunch of static final DataType variables in a class and another by using Enum.

Here is the fist type:

public class TipTipProperties {
    public static final String MAX_WIDTH_AUTO = "auto";
    public static final String POSITION_RIGHT = "right";    
}

And the usage of these variable will via static call, as an example: TipTipProperties.MAX_WIDTH_AUTO

And the second type is:

public enum TipTipProperties {

    MAX_WIDTH_AUTO(MaxWidth.AUTO),  
    POSITION_RIGHT(Position.RIGHT);

    private MaxWidth maxWidth;
    private Position position;  

    private TipTipProperties(MaxWidth maxWidth) {
        this.maxWidth = maxWidth;
    }

    private TipTipProperties(Position position) {       
        this.position = position;
    }

    public MaxWidth getMaxWidth() {
        return maxWidth;
    }   

    public Position getPosition() {
        return position;
    }

    public enum MaxWidth {
        AUTO("auto");

        private String width;

        private MaxWidth(String width) {
            this.width = width;
        }

        public String getWidth() {
            return width;
        }
    }

    public enum Position {
        RIGHT("right"),         

        private String position;

        private Position(String position) {
            this.position = position;
        }

        public String getPosition() {
            return position;
        }       
    }
}

As an example usage: TipTipProperties.POSITION_RIGHT.getPosition().getPosition().

My question is:

  • Which one is better OOP and why?
  • Is there any alternatives or better approach exists?

Thanks in advance.

解决方案

Enum is the best to do this as Joshua Bloch said in Effective Java,you will have more control using Enum like if you want to print all constants,you can. with class constants you can not have type safety.read this for further help

这篇关于如何定义常数值 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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