博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin 构造函数_Kotlin程序| 主要构造函数示例
阅读量:2528 次
发布时间:2019-05-11

本文共 2436 字,大约阅读时间需要 8 分钟。

kotlin 构造函数

主要建设者 (Primary Constructor)

  • A Kotlin class have Primary constructor and one or more Secondary constructor.

    Kotlin类具有Primary构造函数和一个或多个Secondary构造函数。

  • In Kotlin, Primary Constructor is the Part of Class Header.

    在Kotlin中,主要构造函数是类标题的一部分。

  • Syntax:

    句法:

    class 
    constructor(
    ){ // Class Body}
  • The default visibility of the constructor will be public.

    构造函数的默认可见性将是public。

  • Parameter of the primary constructor can be used in property initializer, declared in the class body.

    可以在类体内声明的属性初始化程序中使用主构造函数的参数。

演示Kotlin中主要构造函数示例的程序 (Program to demonstrate the example of Primary Constructor in Kotlin)

// Declare Class, with Primary Constructor keyword, // with one Parameterclass Dog constructor(name:String){    // Used Constructor Parameter to initialize property    // in class body    // String? for nullable property,     // so property also contain null    private var name:String?=name    fun getDogName(): String?{        return name    }}/*    A) Constructor Keyword can be omitted if constructor     does not have annotations and visibility modifiers.*/// Declare class, omitted Constructor Keywordclass Horse (name:String){    // Used Constructor Parameter to     // initialize property in class body    private var name:String =name.toUpperCase()    fun getHorseName(): String?{        return name    }}/*    A) Kotlin has concise Syntax to declaring property     and initialize them from primary constructor.    class Employee(var empName:String, val salary:Int)    B) same way as regular properties declaration,     properties declare in primary constructor can be     var (mutable) or val (immutable) */ // Declare class, Properties declare  in primary constructorclass Cat(private var name:String, private val age:Int){    fun setCatName(name:String){        this.name =name    }    fun printData(){        println("Name : $name and Age : $age")    }}// Main Function, entry Point of Programfun main(args:Array
){ // Create Dog Class Object val dog = Dog("tommy") println("Dog Name : ${dog.getDogName()}") // Create Horse Class object val horse =Horse("Chetak") println("Horse Name : ${horse.getHorseName()}") // Create Cat Class object val cat = Cat("Katrina",32) cat.printData() cat.setCatName("Micy") cat.printData()}

Output:

输出:

Dog Name : tommyHorse Name : CHETAKName : Katrina and Age : 32Name : Micy and Age : 32

翻译自:

kotlin 构造函数

转载地址:http://kexzd.baihongyu.com/

你可能感兴趣的文章
【Python】不定期更新学习小问题整理
查看>>
Zico源代码分析:执行启动过程分析和总结
查看>>
Android之Http通信——1.初识Http协议
查看>>
hdu5044(二分)
查看>>
静态路由、缺省路由的作用
查看>>
linux快捷键绝对路径相对路径讲解
查看>>
又漏一次
查看>>
dede模板中plus文件路径使用方法
查看>>
xml解析demo使用
查看>>
python使用模板手记
查看>>
No result defined for action com.nynt.action.ManageAction and result input问题
查看>>
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
查看>>
洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
查看>>
c++ 连接数据库
查看>>
函数指针与回调函数
查看>>
你所不知道的 CSS 滤镜技巧与细节
查看>>
hack reviewboard 让 FireFox 把 tab 显示为 4 或 2 个空格
查看>>
css 学习整理
查看>>
录制游戏视频——fraps
查看>>
jQuery 停止动画
查看>>