枚举,静态常量,注解的选择

枚举:

枚举类型是强类型的,从而保证了系统安全性。枚举可以限定参数的个数,对调用者的行为能更加严格地进行控制。把一些运行期的参数检查放到了编译期。通俗的来讲就是枚举更加安全,提高了代码的可读性。

下面是有关枚举的一个简单的例子:

public enum Season {

    Spring(1,"春","cyan","warmth"),Summer(2,"夏","green","hot"),
    Autumn(3,"秋","yellow","cool"),Winter(4,"冬","white","cold");

    private int code;
    private String name;
    private String color;
    private String feel;

    Season(int code, String name, String color, String feel) {
        this.code = code;
        this.name = name;
        this.color = color;
        this.feel = feel;
    }

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public String getFeel() {
        return feel;
    }
}

测试方法:

public class CompareEnumTest {

    public static void main(String[] args) {
        Emun(Season.Spring);
    }

    /**
     * 枚举
     */
    public static void Emun(Season season){
        switch (season){
            case Spring:
                System.out.println(Season.Spring.getCode());
                System.out.println(Season.Spring.getName());
                System.out.println(Season.Spring.getColor());
                System.out.println(Season.Spring.getFeel());
                break;
            case Summer:
                System.out.println(Season.Summer.getCode());
                System.out.println(Season.Summer.getName());
                System.out.println(Season.Summer.getColor());
                System.out.println(Season.Summer.getFeel());
                break;
            case Autumn:
                System.out.println(Season.Autumn.getCode());
                System.out.println(Season.Autumn.getName());
                System.out.println(Season.Autumn.getColor());
                System.out.println(Season.Autumn.getFeel());
                break;
            case Winter:
                System.out.println(Season.Winter.getCode());
                System.out.println(Season.Winter.getName());
                System.out.println(Season.Winter.getColor());
                System.out.println(Season.Winter.getFeel());
                break;
        }
    }
}

可以看出,使用枚举在进行参数传递的时候,使用的枚举类型Season的变量,使得代码的可读性提高

但是枚举就一定是完美的吗,其实不然

使用枚举会占用更多的空间(具体情况可以看一下这篇博文 使用枚举为什么更占空间

静态常量:

还有之中方法就是使用静态常量来代替枚举

代码:

public class Season {
    public static final int SPRING=1;
    public static final int SUMMER=2;
    public static final int ATUMN=3;
    public static final int WINTER=4;
}

测试代码:

public class CompareStaticConstantTest {
    public static void main(String[] args) {
        staticConstant(1);
    }

    public static void staticConstant(int seaon){
        switch (seaon){
            case Season.SPRING:
                System.out.println("SPRING do...");
                break;
            case Season.SUMMER:
                System.out.println("SUMMER do...");
                break;
            case Season.ATUMN:
                System.out.println("ATUMN do...");
                break;
            case Season.WINTER:
                System.out.println("WINTER do...");
                break;
        }
    }
}

这样代码明显简洁很多,但是静态常量就没有缺点吗

1.可以看出,使用静态常量是无法存入更多的属性值

使用枚举可以存储code,name,color,feel等信息,而且并没有限制,但是使用静态常量就只能存储code和name两个值

2.还有就是在使用过程中,无法传入的参数进行限制

使用枚举时,传入的参数为为Season的枚举类型的数据,这也就限定了参数的范围,降低了代码出错的可能性

但是使用静态常量的话,传入的只是一个整型数据,如果使用时传递了一个1-4之外的数,程序时不会报错的,但是在运行时就会出错

总结:

枚举提高了代码的安全性和可读性,但是也占用了更多的内存空间

静态变量占用内存更小,但是可存储的数据有限,而且对于代码的可读性也不如枚举

枚举更适合于资源要求不严格或枚举种类不多且枚举需要存储多种属性时使用

静态常量适用于资源要求严格,且存储数据少的情况使用

注解:

除了以上两中方式,还有一种就是注解:

代码:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
public @interface Season {
    int Spring = 1;
    int Summer = 2;
    int Antumn = 3;
    int Winter = 4;
}

测试代码:

public class CompareAnnotationTest {
    public static void main(String[] args) {
        annotation(1);
    }


    public static void annotation(@Season int season){
        switch (season){
            case StaticConstant.Season.SPRING:
                System.out.println("SPRING do...");
                break;
            case StaticConstant.Season.SUMMER:
                System.out.println("SUMMER do...");
                break;
            case StaticConstant.Season.ATUMN:
                System.out.println("ATUMN do...");
                break;
            case StaticConstant.Season.WINTER:
                System.out.println("WINTER do...");
                break;
        }
    }
}

使用注解的不仅可以提高代码的可读性,还可以节省更多的空间

而且在安卓开发中更多使用的时注解而不是枚举,所以在开发中应该尽可能的使用注解,而不是枚举

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值