当前位置:网站首页>kotlin 类和对象
kotlin 类和对象
2022-07-04 18:32:00 【AdleyTales】
import java.util.*
// 类包含构造函数、初始化代码块、函数、属性、内部类、对象声明
// 非空属性必须在定义的时候初始化,kotlin提供了一种可以延迟初始化的方案,使用 lateinit 关键字描述属性
class People {
// 类的属性可以用关键字 var 声明为可变的,否则使用只读关键字 val 声明为不可变
var name: String = ""
var age: Int = 0
var addr: String = ""
get() = field.uppercase()
set(value) {
field = "G+$value"
}
constructor(name: String, age: Int, addr: String) {
this.name = name
this.age = age
this.addr = addr
println("constructor------")
}
constructor(name: String, age: Int) {
this.name = name
this.age = age
}
init {
println("init ------")
}
// 成员函数
fun foo() {
println("foo ---")
}
fun info() {
println("name: $name, age: $age, addr: $addr")
}
}
fun main() {
val p = People("adleytales", 19, "Beijing")
p.foo()
p.info()
p.name = "adley"
p.age = 18
p.info()
}
边栏推荐
- Educational codeforces round 22 E. Army Creation
- 测试工程师如何“攻城”(上)
- Bi skills - permission axis
- The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
- BCG 使用之CBCGPProgressDlg进度条使用
- MySQL数据库基本操作-DDL | 黑马程序员
- Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
- 如何使用Async-Awati异步任务处理代替BackgroundWorker?
- FPGA timing constraint sharing 01_ Brief description of the four steps
- 数组中的第K个最大元素
猜你喜欢
Use canal and rocketmq to listen to MySQL binlog logs
Hough Transform 霍夫变换原理
Stream流
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
Online sql to excel (xls/xlsx) tool
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
Some thoughts on whether the judgment point is located in the contour
Stream stream
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
To sort out messy header files, I use include what you use
随机推荐
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
How test engineers "attack the city" (Part I)
项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理
欧拉函数
Pointnet/Pointnet++点云数据集处理并训练
矩阵翻转(数组模拟)
1002. A+b for Polynomials (25) (PAT class a)
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
在线SQL转Excel(xls/xlsx)工具
Shell 编程核心技术《一》
安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
求2的n次方
Several methods of online database migration
有关架构设计的个人思考(本文后续不断修改更新)
node_exporter部署
1002. A+B for Polynomials (25)(PAT甲级)
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总