kotlin 构造函数_Kotlin程序| 主要构造函数示例
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程序| 主要构造函数示例相关推荐
- kotlin 字符串_Kotlin程序确定字符串是否具有所有唯一字符
kotlin 字符串 Given a string, we have to check whether it has all unique characters or not. 给定一个字符串,我们必 ...
- kotlin 类和对象_Kotlin程序| 类和对象的示例(带有学生数据)
kotlin 类和对象 In the below program, we are creating a student class to input and print the student dat ...
- kotlin 类构造函数_Kotlin类– Kotlin构造函数
kotlin 类构造函数 In this tutorial, we'll be discussing the Object Oriented Programming concepts of Kotli ...
- 解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法
解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法 参考文章: (1)解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法 ...
- kotlin 第一个程序_Kotlin程序添加两个矩阵
kotlin 第一个程序 Given two matrices, we have to add them. 给定两个矩阵,我们必须将它们相加. Example: 例: Input: matrix 1: ...
- kotlin 计算平方_Kotlin程序来计算复利
kotlin 计算平方 Compound interest is the sum of principal amount and interest of interest. 复利是本金和利息之和. G ...
- kotlin 第一个程序_Kotlin程序减去两个矩阵
kotlin 第一个程序 Given two matrices, we have to subtract them. 给定两个矩阵,我们必须将它们相减. Example: 例: Input: matr ...
- kotlin 扩展类的功能_Kotlin程序| 扩展功能功能
kotlin 扩展类的功能 扩展功能 (Extension function) Kotlin provides the ability to add more functionality to the ...
- java的构造函数详解,Java构造函数与普通函数用法详解
函数也被称为方法! 函数的作用及特点: 1.用于定义功能,将功能封装. 2.可以提高代码的复用性. 函数注意事项: 1.不能进行函数套用(不可以在函数内定义函数). 2.函数只有被调用才能被执行. 3 ...
最新文章
- UIAlertView
- R可视化绘制卡方分布图(Chi-Square Distribution)
- 特征锦囊:怎么批量把特征中的离群点给处理下?
- python意外缩进引发逻辑错误_如何编写 Python 程序
- activemq发布订阅
- singer页左侧滚动的时候右侧跟随高亮显示
- 人工智能python营_贪心学习院人工智能python编程特训营
- Ubuntu16.04安装matlab 2015b教程(含下载和激活文件)
- selenium + java po模式
- Atitit.跨语言标准化 web cgi api v2 saa CGI (通用网关接口) 编辑 CGI 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能。CG
- 【PTA】中M22春C、Java入门练习7-138 质因子分解
- C语言程序设计(朱立华 陈可佳)
- python学习之天天向上代码
- android编程播放音乐,Android编程实现播放音频的方法示例
- pdf编辑软件哪个好?编辑pdf的软件分享一款,像word一样好用!
- linux 保存文件名乱码怎么办,Linux 文件名编码转换 乱码 解决办法
- Java中限定类名和非限定类名的区别
- IMU之磁力计校准地磁场计算磁航向
- JavaScript 获取当前日期和三十天以前日期
- centos中nginx使用