跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 在 kotlin 中有必要用常量注解吗?
未分類
22 7 月 2020

在 kotlin 中有必要用常量注解吗?

在 kotlin 中有必要用常量注解吗?

資深大佬 : w950888 9

在 kotlin 中有必要用常量注解吗?如果需要用下面的常量类用 kotlin 应该怎么写呢,直接转换 kotlin 会报错。

import androidx.annotation.StringDef; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;  /**  * @author Luke  * @version 1.0.0  * @time 2020/7/13 2:14 PM  * @description 常量池  */ public class Constant {      @IntDef({             AlertCode.SYSTEM,     })     @Documented     @Retention(RetentionPolicy.SOURCE)     @Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})     public @interface AlertCode {         int SYSTEM = 0;     }  } 

大佬有話說 (10)

  • 資深大佬 : gz911122

    这是干啥用的啊?

  • 主 資深大佬 : w950888

    @gz911122 常量池,同时具有类似枚举限定范围的作用,要比枚举性能好

  • 資深大佬 : gz911122

    @w950888 哦我想起来了 加在参数上, 限制输入的…

    android 专用, 后端没有…

  • 資深大佬 : lianyue13

    “`kotlin
    class Constant {
    @IntDef(AlertCode.SYSTEM)
    @Documented
    @Retention(AnnotationRetention.SOURCE)
    @Target(AnnotationTarget.LOCAL_VARIABLE, AnnotationTarget.FIELD, AnnotationTarget.FUNCTION)
    annotation class AlertCode {
    companion object {
    const val SYSTEM = 0
    }
    }
    }
    “`

  • 資深大佬 : sagaxu

    @w950888 jake wharton 说 enum 和 int 性能一样

  • 資深大佬 : loshine1992

    ART 之后直接用枚举就可以了

  • 主 資深大佬 : w950888

    @lianyue13 非常感谢

  • 主 資深大佬 : w950888

    @sagaxu 在 Android 里 @IntDef 比枚举更省内存

  • 資深大佬 : sagaxu

    @w950888 省 dex 字节码,但不省内存。ART 会自动优化,运行时 enum 和 int 没区别。

  • 主 資深大佬 : w950888

    @sagaxu (* ̄︶ ̄) 懂了,谢谢哈

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具