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. 利用select实现年月日三级联动的日期选择效果
  2. ios_UITextField右侧小圆叉
  3. C++ Opengl Fog(雾)源码
  4. sequelize连接mysql_node.js通过Sequelize 连接MySQL
  5. 2016年10个重要的可视化发展
  6. UI设计素材模板|wireframe线框图设计要点
  7. Flowable官方指定中国社区成立了
  8. Linux部署东方通TongWeb7
  9. opencv 图像平滑处理(python)
  10. QT的自动滚动区QScrollArea的用法,图文详解
  11. 安徽农业大学计算机考研分数线,安徽农业大学考研录取分数线
  12. jquery 3D旋转效果
  13. 5个小技巧,让你的for循环瞬间高大上!
  14. python入门之运算符的使用的答案_python基础课程 第2章 (运算符)
  15. python中的可迭代是什么意思_Python可迭代跟迭代器的区别
  16. 轮播一页显示多少条数据
  17. 备案 - 多个域名同时备案
  18. 深度学习网络各种激活函数 Sigmoid、Tanh、ReLU、Leaky_ReLU、SiLU、Mish
  19. wait waitpid waitid wait3 wait4
  20. Solaris 常见问题

热门文章

  1. Xctf练习sql注入--supersqli
  2. oracle查看context,oracle context(上下文)
  3. python中用函数设计栈的括号匹配问题_数据结构和算法(Python版):利用栈(Stack)实现括号的匹配问题...
  4. python wget安装_Macbook系统环境安装wget的2个方法 - 传统包及Homebrew安装
  5. matlab 子图title的位置_matlab 画图基本介绍
  6. python 当前文件路径获取方式_Python获取当前文件路径
  7. window.open不重复打开同一个名称的窗口_干货满满|Ctrl键的正确打开方式
  8. 离线安装 Pytorch 1.2.0 torchvision 0.3.0
  9. oracle迁移父子数据
  10. Angular之ngx-permissions的管理权限