码迷,mamicode.com
首页 > 其他好文 > 详细

枚举三部曲之一

时间:2018-01-10 15:30:48      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:problems   情况下   compile   允许   tps   single   静态内部类   htm   class   

##什么是枚举,特点,使用场景 It is a compile-time error if an enum declaration has the modifier abstract or final.

  • 不能被继承,不能修改,否则会编译错误;
  • 枚举作为内部类时,默认静态的,不用声明为 static;
  • 可以在非静态内部类中声明一个枚举类型的变量,而正常情况下非静态内部类中是不允许有静态变量的

##洪水猛兽? https://developer.android.com/topic/performance/memory.html#Overhead

For example, enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

.

A single reference to an int constant (copy of the int itself) also occupies 4 bytes. – BladeCoder Jul 22 ‘15 at 10:26

@BladeCoder reference to enum instance occupy 4 bytes, but instance occupy additional space itself, it‘s much more

.

Although I appreciate the other answers providing pros/cons of enums, this answer should be the accepted answer since it provides the best solution for Android, specifically. Support annotations are the way to go. We, as Android developers should be thinking "unless I have a strong reason to use enums, I will use constants with annotations", rather than the other answers‘ way of thinking "unless I start to get worried about memory/performance later on, I can use enums". Stop yourself now from having performance problems later on! ##其他相似功能实现方式:静态常量,接口,注解

##Thanks: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9

http://gyht0808.iteye.com/blog/489723#comments

枚举三部曲之一

标签:problems   情况下   compile   允许   tps   single   静态内部类   htm   class   

原文地址:https://www.cnblogs.com/shizhijie/p/8258582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!