kotlin 类和对象

In the below program, we are creating a student class to input and print the student data like name, age. It is a simple example of creating class in Kotlin.

在下面的程序中,我们将创建一个学生班级,以输入和打印学生数据,例如姓名,年龄。 这是在Kotlin中创建类的简单示例。

Note:

注意:

  • The compiler creates a default constructor if we do not declare a constructor.

    如果我们不声明构造函数,则编译器将创建默认构造函数。

  • Class properties must be initialized.

    类属性必须初始化。

Kotlin学生课程 (Program for student class in Kotlin)

package com.includehelp
// Class declaration,
class Student{
//member variables of class
private var name: String=""
private var age: Int=0
//Member functions of class to set student name
fun setStudentName(name:String){
this.name=name
}
//Member functions of class to set student age
fun setStudentAge(age:Int){
this.age=age
}
//Member functions of class to return student details
fun getStudentDetails():String{
return "Name :  $name, Age : $age"
}
}
//Main function, Entry Point of Program
fun main(args:Array<String>){
//Create Object of Student Class
val student1 = Student() // There is no 'new' keyword
// set Student age and name to call member functions of class
student1.setStudentName("Mike")
student1.setStudentAge(30)
//print Student details bt ccall getStudentDetails members functions
println("Student : ${student1.getStudentDetails()}")
//Create Second object of student class
val student2 = Student()
student2.setStudentName("Irina")
student2.setStudentAge(23)
println("Student : ${student2.getStudentDetails()}")
}

Output:

输出:

Student : Name :  Mike, Age : 30
Student : Name :  Irina, Age : 23

翻译自: https://www.includehelp.com/kotlin/example-of-class-and-object-with-student-data.aspx

kotlin 类和对象

kotlin 类和对象_Kotlin程序| 类和对象的示例(带有学生数据)相关推荐

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

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

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

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

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

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

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

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

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

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

  6. kotlin 编译时常量_Kotlin程序| 编译时常量示例

    kotlin 编译时常量 编译时常数 (Compile-time Constant) If the value of a read-only (immutable) property is known ...

  7. kotlin 16进制_Kotlin程序将八进制数转换为十进制数

    kotlin 16进制 Given a number in octal number system format, we have to convert it into decimal number ...

  8. kotlin 字符串去空格_Kotlin程序从字符串中删除所有空格

    kotlin 字符串去空格 Given a string, we have to remove all whitespaces from it. 给定一个字符串,我们必须从中删除所有空格. Examp ...

  9. php程序员好找对象吗,程序员找对象那么难吗

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 其实这些话想说很久了,只是一直觉得不合适,或者说还想留有一丝希望,也不知道现在是什么想法,也许我想在我改变之前,想留下点什么吧.不知道你看到这个后会是什么 ...

最新文章

  1. 腾讯面试题:创建索引时,你会怎么考虑呢?(看完你就能和面试官谈人生了)
  2. cisco vrrp
  3. 【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )
  4. 选择纯种犬还是杂种犬?
  5. 图解HTTP学习记录(六)
  6. meta name=viewport content=width=device-width,minimum-scale=1.0,maximum-scale=1.0/
  7. dubbo-go v1.5.6来喽!
  8. translate动画
  9. 10一个应用阻止关机贴吧_手机该不该每天关机一次?看完才知道这么多年白用了...
  10. leetcode 26 --- removeDuplicates
  11. PLCrashReporter使用
  12. 学生信息管理系统_C语言实现_单链表
  13. Selenium下载页面上的图片
  14. vue项目中使用词云_如何在vue项目中使用高拍仪
  15. Gartner发布《2023年十大战略技术趋势》
  16. 更完美 联想乐Phone获取root权限教程
  17. 对话阿里云张建锋:云计算正在内卷吗?丨钛度专访
  18. 大学计算机专业找对象,单身率最高的大学专业是什么?这5个专业为什么成脱单最难专业...
  19. java 自省_javabean的自省机制
  20. 《星际争霸》单位语音中英文完全版

热门文章

  1. python mysql删除数据_python-mysql删除和更新数据
  2. html制作卡通图案代码,CSS画的卡通动画图案
  3. matlab 子图title的位置_matlab 画图基本介绍
  4. C语言case字句有什么作用,switch case 语句的使用规则
  5. php 支付签名验证失败,choosewxpay fail解决,微信支付签名验证错误解决
  6. percona-toolkit---pt-heartbeat
  7. git和gitlab安装
  8. 安装 Docker Machine
  9. 整数数组按绝对值排序
  10. java.util (Collection接口和Map接口)