博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Java中定义常量
阅读量:5992 次
发布时间:2019-06-20

本文共 2427 字,大约阅读时间需要 8 分钟。

 

方法一采用接口(Interface)的中变量默认为static final的特性。

 

方法二采用了Java 5.0中引入的Enum类型。

 

方法三采用了在普通类中使用static final修饰变量的方法。

 

方法四类似方法三,但是通过函数来获取常量。

/** * Method One */interface ConstantInterface {    String SUNDAY = "SUNDAY";    String MONDAY = "MONDAY";    String TUESDAY = "TUESDAY";    String WEDNESDAY = "WEDNESDAY";    String THURSDAY = "THURSDAY";    String FRIDAY = "FRIDAY";    String SATURDAY = "SATURDAY";}/** * Method Two  */enum ConstantEnum {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}/** * Method Three */class ConstantClassField {    public static final String SUNDAY = "SUNDAY";    public static final String MONDAY = "MONDAY";    public static final String TUESDAY = "TUESDAY";    public static final String WEDNESDAY = "WEDNESDAY";    public static final String THURSDAY = "THURSDAY";    public static final String FRIDAY = "FRIDAY";    public static final String SATURDAY = "SATURDAY";}/** * Method Four * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html */class ConstantClassFunction {    private static final String SUNDAY = "SUNDAY";    private static final String MONDAY = "MONDAY";    private static final String TUESDAY = "TUESDAY";    private static final String WEDNESDAY = "WEDNESDAY";    private static final String THURSDAY = "THURSDAY";    private static final String FRIDAY = "FRIDAY";    private static final String SATURDAY = "SATURDAY";    public static String getSunday() {        return SUNDAY;    }    public static String getMonday() {        return MONDAY;    }    public static String getTuesday() {        return TUESDAY;    }    public static String getWednesday() {        return WEDNESDAY;    }    public static String getThursday() {        return THURSDAY;    }    public static String getFirday() {        return FRIDAY;    }    public static String getSaturday() {        return SATURDAY;    }}public class TestConstant {    static final String day = "saturday";    public static void main(String[] args) {        System.out.println("Is today Saturday?");        System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY));        System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name()));        System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY));        System.out.println(day.equalsIgnoreCase(ConstantClassFunction                .getSaturday()));    }}

 

转载于:https://www.cnblogs.com/Nyan-Workflow-FC/p/4968159.html

你可能感兴趣的文章
用Next.js快速上手React服务器渲染
查看>>
Spring IOC 收藏
查看>>
MySQL数据库事务各隔离级别加锁情况--read committed && MVCC
查看>>
Maven的编译插件
查看>>
HttpClient使用教程
查看>>
js的初始化---$(function () {//加载后 })
查看>>
java ldap读取方法
查看>>
python3学习日志 多线程编程
查看>>
技术宅学习Linux系统还是很有前途的
查看>>
Java读取Properties文件的六种方法
查看>>
Centos7 安装pyenv
查看>>
nginx反向代理后应用程序如何获取客户端真实IP?
查看>>
text-align:justify实现两端对齐布局
查看>>
JVM总结_JVM内存结构
查看>>
IntelliJ IDEA 使用教程十二 - Debug 调试多线程程序
查看>>
异常处理
查看>>
Ubuntu里node命令无效解决方法
查看>>
《将博客搬至CSDN》
查看>>
各种资源网站
查看>>
js二维码生成插件“jquery.qrcode.min.js”
查看>>