kotlin 编译时常量_Kotlin程序| 编译时常量示例
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程序| 编译时常量示例相关推荐
- 单片机c语言编译过程,单片机C程序编译、执行过程
1.编写C代码: 1)预编译代码 2)定义及声明变量 3)定义及声明函数 4)编写程序代码(主函数MAIN) 2.编译器编译: 1) 预处理 此时编译器会扫描一个一个的C源文件,如果发现#includ ...
- kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例
kotlin 覆盖属性 方法重载 (Method Overriding) Method overriding allows derived class has the same function na ...
- kotlin创建静态单利_Kotlin程序来计算单利
kotlin创建静态单利 Given, principal, rate, and time, we have to calculate the simple interest. 给定本金,利率和时间, ...
- python文件编译_将c程序编译为python扩展,生成.whl文件
借鉴的文章会在文章末尾会逐一给出 1. 对c文件进行编译 先确保要实现的代码运行成功,然后将main()函数删除,例如test.c文件 void test_func(char *str){ FILE ...
- phonegap编译Android,PhoneGap应用程序编译
现在我们可以编译第一个网络API基于快速模式的应用程序.在这最后的环节中,我们将学习转化我们的网络内容的应用程序格式,可以在在线应用程序商店上载的过程. PhoneGap接受GitHub上或使用Ado ...
- kotlin中判断字符串_Kotlin程序查找字符串中字符的频率
kotlin中判断字符串 Given a string and a character, we have to find the frequency of the character in the s ...
- kotlin半生对象_Kotlin程序| 随播对象特征
kotlin半生对象 伴侣对象 (Companion object) If you need a function or a property to be tied to a class rather ...
- kotlin 类和对象_Kotlin程序| 类和对象的示例(带有学生数据)
kotlin 类和对象 In the below program, we are creating a student class to input and print the student dat ...
- kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符
kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...
最新文章
- 【超越白皮书3】DAG技术解析与实测
- Python-SQLAlchemy:第4节:级联
- 10款jQuery/CSS3动画应用 超有用
- ubuntu 配置samba
- ogre3D学习基础10 -- 键盘控制与鼠标控制(直接控制)
- hive sql 学习笔记
- python中文词云图代码_Python简单实现词云图代码及步骤解析
- win10怎么在网络里查看win7计算机,网络共享中win7能找到win10,但win10找不到win7
- python中sorted函数的用法_Python中map,reduce,filter和sorted函数的使用方法
- 解决办法:KeyError: ‘ExpandBackward’及老版本pytorch/torchvision的安装办法。
- Mybatis的简单介绍,什么是Mybatis框架
- Linux开机密码重置
- word找不到橡皮擦工具
- Day08 - HTML5 Canvas 实现彩虹画笔绘画板指南
- 程序员真的需要一台 Mac 吗?
- 398、Java框架52 -【Hibernate - 分页、两种获取方式】 2020.10.27
- CentOS下安装cups实现局域网共享HP1020打印机
- 计算机考研815指什么,考研815化学包括什么
- 产品必备技能(十一):如何写产品分析报告?附实产品分析报告实例(闲鱼)
- Udemy上最好的JavaScript课程之一,强烈推荐新手入门学习