必威体育Betway必威体育官网
当前位置:首页 > IT技术

在java类中定义常量的三中方法

时间:2019-06-16 12:43:10来源:IT技术作者:seo实验室小编阅读:62次「手机版」
 

java常量

主要讲解如何在java类中定义常量:

/**
 * 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/<a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>/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/DreamDrive/p/5413255.html

相关阅读

JavaWeb(EL表达式学习笔记)

EL表达式 一、什么是EL: 1.1、EL – Expression Language 表达式语言 1.2、语法: ${EL表达式} 1.3、与JSP表达式区别: JSP

Java 正则表达式

介绍 前段时间使用Java的正则表达式做一些字符串匹配的任务,现将学到的正则表达式的知识整理成文。 Java中Spring.class涉及到正

Java语言有哪些优点

1、Java为纯面向对象的语言。 2、Java语言具有平台无关性。Java为解释性语言,编译器会把Java代码变成“中间字节码”,然后在Java虚

一些Java反编译工具/源代码查看工具的介绍

一些Java反编译工具/源代码查看工具的介绍摘要: 有的朋友抱怨他们在使用他们公司的闭源框架时看不到底层的源代码。那么可以尝试

JavaFX学习篇---1、IDEA配置JavaFX开发环境和创建Java

以下操作过程参照官方文档实践,https://www.jetbrains.com/help/idea/preparing-for-javafx-application-development.html 1、安

分享到:

栏目导航

推荐阅读

热门阅读