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 <Class Name> constructor(<optional Parameters>){
    // 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 Parameter
class 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 Keyword
class 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 constructor
class 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 Program
fun main(args:Array<String>){
// 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 : tommy
Horse Name : CHETAK
Name : Katrina and Age : 32
Name : Micy and Age : 32

翻译自: https://www.includehelp.com/kotlin/example-of-primary-constructor.aspx

kotlin 构造函数

kotlin 构造函数_Kotlin程序| 主要构造函数示例相关推荐

  1. kotlin 字符串_Kotlin程序确定字符串是否具有所有唯一字符

    kotlin 字符串 Given a string, we have to check whether it has all unique characters or not. 给定一个字符串,我们必 ...

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

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

  3. kotlin 类构造函数_Kotlin类– Kotlin构造函数

    kotlin 类构造函数 In this tutorial, we'll be discussing the Object Oriented Programming concepts of Kotli ...

  4. 解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法

    解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法 参考文章: (1)解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法 ...

  5. kotlin 第一个程序_Kotlin程序添加两个矩阵

    kotlin 第一个程序 Given two matrices, we have to add them. 给定两个矩阵,我们必须将它们相加. Example: 例: Input: matrix 1: ...

  6. kotlin 计算平方_Kotlin程序来计算复利

    kotlin 计算平方 Compound interest is the sum of principal amount and interest of interest. 复利是本金和利息之和. G ...

  7. kotlin 第一个程序_Kotlin程序减去两个矩阵

    kotlin 第一个程序 Given two matrices, we have to subtract them. 给定两个矩阵,我们必须将它们相减. Example: 例: Input: matr ...

  8. kotlin 扩展类的功能_Kotlin程序| 扩展功能功能

    kotlin 扩展类的功能 扩展功能 (Extension function) Kotlin provides the ability to add more functionality to the ...

  9. java的构造函数详解,Java构造函数与普通函数用法详解

    函数也被称为方法! 函数的作用及特点: 1.用于定义功能,将功能封装. 2.可以提高代码的复用性. 函数注意事项: 1.不能进行函数套用(不可以在函数内定义函数). 2.函数只有被调用才能被执行. 3 ...

最新文章

  1. UIAlertView
  2. R可视化绘制卡方分布图(Chi-Square Distribution)
  3. 特征锦囊:怎么批量把特征中的离群点给处理下?
  4. python意外缩进引发逻辑错误_如何编写 Python 程序
  5. activemq发布订阅
  6. singer页左侧滚动的时候右侧跟随高亮显示
  7. 人工智能python营_贪心学习院人工智能python编程特训营
  8. Ubuntu16.04安装matlab 2015b教程(含下载和激活文件)
  9. selenium + java po模式
  10. Atitit.跨语言标准化 web cgi api v2 saa CGI   (通用网关接口)  编辑 CGI 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能。CG
  11. 【PTA】中M22春C、Java入门练习7-138 质因子分解
  12. C语言程序设计(朱立华 陈可佳)
  13. python学习之天天向上代码
  14. android编程播放音乐,Android编程实现播放音频的方法示例
  15. pdf编辑软件哪个好?编辑pdf的软件分享一款,像word一样好用!
  16. linux 保存文件名乱码怎么办,Linux 文件名编码转换 乱码 解决办法
  17. Java中限定类名和非限定类名的区别
  18. IMU之磁力计校准地磁场计算磁航向
  19. JavaScript 获取当前日期和三十天以前日期
  20. centos中nginx使用

热门文章

  1. Git初始化配置以及配置github
  2. 链表!比数组更适合做增删操作的数据结构
  3. 动态规划:从新手到专家
  4. C语言结构体及函数传递数组參数演示样例
  5. 初识openstack
  6. js中的this指针(二)
  7. javascript数据结构-栈
  8. Asp.net MVC 4新项目中创建area的后续操作
  9. 第4章 maven依赖特性
  10. pat00-自测5. Shuffling Machine (20)