javaconstant类_创建java常量类

更新时间:2023-05-25 20:37:18 阅读: 评论:0

javaconstant类_创建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
* /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.equalsIgnoreCa(ConstantInterface.SATURDAY)); System.out.println(day.equalsIgnoreCa(ConstantEnum.SATURDAY.name())); System.out.println(day.equalsIgnoreCa(ConstantClassField.SATURDAY)); System.out.println(day.equalsIgnoreCa(ConstantClassFunction
.getSaturday()));
}
}
/**
* 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
* /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.equalsIgnoreCa(ConstantInterface.SATURDAY));
System.out.println(day.equalsIgnoreCa(ConstantEnum.SATURDAY.name()));
System.out.println(day.equalsIgnoreCa(ConstantClassField.SATURDAY));
System.out.println(day.equalsIgnoreCa(ConstantClassFunction
.getSaturday()));
}
}
⽅法⼀采⽤接⼝(Interface)的中变量默认为static final的特性。
⽅法⼆采⽤了Java 5.0中引⼊的Enum类型。
⽅法三采⽤了在普通类中使⽤static final修饰变量的⽅法。
⽅法四类似⽅法三,但是通过函数来获取常量。
⾸先定义全局变量似乎有违Java的⾯向对象的封装特性,增加的耦合。所以最佳的⽅法是避免定义全局变量。如果是参数等,可以写⼊配置⽂件。如果实在是必须的,⽅法⼆是最为推荐的。⽅法三是⼤家都能想到的,⾮常的直观。⽅法⼀和⽅法三本质上⼀样。⽅法四提供了灵活性,具体参考引⽤【1】。
怎么编辑

本文发布于:2023-05-25 20:37:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/775254.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:全局变量   法三   灵活性   变量   常量
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图