kotlin 编译时常量

编译时常数 (Compile-time Constant)

  • If the value of a read-only (immutable) property is known at the compile time.

    如果在编译时已知只读(不可变)属性的值。

  • Mark it as a compile-time constant using the const modifier.

    使用const修饰符将其标记为编译时常量。

  • Such properties must be fulfilled by the following requirements.

    这些特性必须满足以下要求。

    • Top-level, or member of an object declaration or a companion object.
    • Initialized with a value of type String or a primitive type
    • No custom getter
  • No run time assignment allowed into const variables.

    不允许将运行时分配分配给const变量。

  • The val keyword also used to make property immutable but the main difference between const and val is that properties declare with val can be initialized at runtime.

    val关键字还用于使属性不可变,但是const和val之间的主要区别在于,可以在运行时初始化使用val声明的属性。

Kotlin中的编译时常数程序 (Program for compile-time constant in Kotlin)

package com.includehelp
//declare Top Level compile time constant
const val PI=3.14
//Declare Class with object to make singleton
object Physics{
//declare compile time constant
const val GRAVITY=10
}
//declare class
class Greetings{
//declare companion object
companion object{
//declare compile time constant
const val GREET="Hello IncludeHelp"
}
}
//Main Function, Entry Point of Program
fun main(){
//Print All Constant Value
println("PI Value : $PI")
println("Gravity  : ${Physics.GRAVITY}")
println("Greetings: ${Greetings.GREET}")
}

Output:

输出:

PI Value : 3.14
Gravity  : 10
Greetings: Hello IncludeHelp

翻译自: https://www.includehelp.com/kotlin/example-of-compile-time-constant.aspx

kotlin 编译时常量

kotlin 编译时常量_Kotlin程序| 编译时常量示例相关推荐

  1. 单片机c语言编译过程,单片机C程序编译、执行过程

    1.编写C代码: 1)预编译代码 2)定义及声明变量 3)定义及声明函数 4)编写程序代码(主函数MAIN) 2.编译器编译: 1) 预处理 此时编译器会扫描一个一个的C源文件,如果发现#includ ...

  2. kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例

    kotlin 覆盖属性 方法重载 (Method Overriding) Method overriding allows derived class has the same function na ...

  3. kotlin创建静态单利_Kotlin程序来计算单利

    kotlin创建静态单利 Given, principal, rate, and time, we have to calculate the simple interest. 给定本金,利率和时间, ...

  4. python文件编译_将c程序编译为python扩展,生成.whl文件

    借鉴的文章会在文章末尾会逐一给出 1. 对c文件进行编译 先确保要实现的代码运行成功,然后将main()函数删除,例如test.c文件 void test_func(char *str){ FILE ...

  5. phonegap编译Android,PhoneGap应用程序编译

    现在我们可以编译第一个网络API基于快速模式的应用程序.在这最后的环节中,我们将学习转化我们的网络内容的应用程序格式,可以在在线应用程序商店上载的过程. PhoneGap接受GitHub上或使用Ado ...

  6. kotlin中判断字符串_Kotlin程序查找字符串中字符的频率

    kotlin中判断字符串 Given a string and a character, we have to find the frequency of the character in the s ...

  7. kotlin半生对象_Kotlin程序| 随播对象特征

    kotlin半生对象 伴侣对象 (Companion object) If you need a function or a property to be tied to a class rather ...

  8. kotlin 类和对象_Kotlin程序| 类和对象的示例(带有学生数据)

    kotlin 类和对象 In the below program, we are creating a student class to input and print the student dat ...

  9. kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

    kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...

最新文章

  1. 【超越白皮书3】DAG技术解析与实测
  2. Python-SQLAlchemy:第4节:级联
  3. 10款jQuery/CSS3动画应用 超有用
  4. ubuntu 配置samba
  5. ogre3D学习基础10 -- 键盘控制与鼠标控制(直接控制)
  6. hive sql 学习笔记
  7. python中文词云图代码_Python简单实现词云图代码及步骤解析
  8. win10怎么在网络里查看win7计算机,网络共享中win7能找到win10,但win10找不到win7
  9. python中sorted函数的用法_Python中map,reduce,filter和sorted函数的使用方法
  10. 解决办法:KeyError: ‘ExpandBackward’及老版本pytorch/torchvision的安装办法。
  11. Mybatis的简单介绍,什么是Mybatis框架
  12. Linux开机密码重置
  13. word找不到橡皮擦工具
  14. Day08 - HTML5 Canvas 实现彩虹画笔绘画板指南
  15. 程序员真的需要一台 Mac 吗?
  16. 398、Java框架52 -【Hibernate - 分页、两种获取方式】 2020.10.27
  17. CentOS下安装cups实现局域网共享HP1020打印机
  18. 计算机考研815指什么,考研815化学包括什么
  19. 产品必备技能(十一):如何写产品分析报告?附实产品分析报告实例(闲鱼)
  20. Udemy上最好的JavaScript课程之一,强烈推荐新手入门学习

热门文章

  1. python可以在多平台运行 体现了_Python:使用异常处理来判断运行的平台
  2. zabbix巡检脚本
  3. Redis(四):String字符串数据类型详解
  4. PACKING【二维01背包】
  5. Cheerleaders【容斥】
  6. JavaWeb学习笔记(九)--HttpServletResponse
  7. spring入门到放弃——spring事务管理
  8. 10.8-全栈Java笔记:序列化/反序列化的步骤和实例
  9. fragment和Activity同时操作UI引起的延迟、卡顿
  10. Struts1.x在MyEclipse中的环境搭建和配置