什么时候可以在Android中使用Enum? [英] When is it good to use Enum in Android?

查看:65
本文介绍了什么时候可以在Android中使用Enum?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了视频( https://www.youtube.com/watch?v=Hzs6OBcvNQE)从Google发布的有关枚举价格的信息,我坚信枚举的成本更高并且存在性能问题.

I saw the video(https://www.youtube.com/watch?v=Hzs6OBcvNQE) posted from google about price of enum and I'm convinced that enum cost more and has performance issue.

但是,当我需要在枚举中包含多个信息时该怎么办?我必须创建intdef和stringdef来映射消息吗?

However, what about when I need to contain multiple information in an enum? Do I have to create intdef and stringdef to map the message?

即.

public enum Error{
    NETWORK(1, "Network error!"),
    STACK_OVER_FLOW(2, "Stack over flow error!");
    final int mValue;
    final String mMessage;

    Error(int value, String message){
        mValue = value;
        mMessage = message;
    }

    public void getMessage(){
        return mMessage;
    }

    public void getValue(){
        return mValue
    }
}

推荐答案

枚举确实具有该内存占用量,但是就好像是大量使用它们,还是像对其进行比较一样使用它们,或者如果您正在使用它们,使用代码中这样的功能,与它们一起使用是合理的,如果您要完全避免枚举,可以将其更改为类.

It is true that enums have that memory footprint, but just if are heavily using them, or sub using them, like comparisions or else, but if you're using such functionality like the one in your code, it's more than justified to work with them, in case you want to totally avoid enums, you can change this to a class.

这篇关于什么时候可以在Android中使用Enum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆