关于常量,比如审核状态,1代表审核通过,2代表审核不通过
第一种使用接口:
public interface Constants{
public static final int AUDIT_STATUS_PASS = 1;
public static final int AUDIT_STATUS_NOT_PASS = 2;
}
第二种使用类:
public class Constans{
public static final int AUDIT_STATUS_PASS = 1;
public static final int AUDIT_STATUS_NOT_PASS = 2;
}
第三种使用枚举:
public enum Constants {
AUDIT_STATUS_PASS(1),
AUDIT_STATUS_NOT_PASS(2);
private int status;
private Constants(int status){
this.setStatus(status);
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
这三种方法有什么区别吗?在实际运用中应该使用什么定义常量更好?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
第一种和第二种是一样的,第一种写起来更方便,不用
public static final,直接int AUDIT_STATUS_PASS = 1就行。第三种好在能把说明也写在里面,比如建议使用枚举。《Effective Java》中也是推荐使用枚举代替
int常量的。实际开发中都是使用枚举,因为耦合更小,更纯粹。
参考:StackOverFlow
枚举当然是首选,另如果不用枚举,在《Effective Java》一书中,作者建议使用一般类加私有构造方法的方式,至于为什么不用接口,那就要上升到语言哲学问题了。