当前位置:网站首页>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()
}
边栏推荐
- Have you guys ever used CDC direct Mysql to Clickhouse
- @transactional滥用导致数据源连接池耗尽问题
- mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
- 如何使用Async-Awati异步任务处理代替BackgroundWorker?
- Functional interface
- Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
- 26. 删除有序数组中的重复项 C#解答
- Shell programming core technology II
- 1672. Total assets of the richest customers
- Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)
猜你喜欢

Wireshark网络抓包

C# 使用StopWatch测量程序运行时间

YOLOv5s-ShuffleNetV2

English grammar_ Noun - use
牛客小白月赛7 谁是神箭手

欧拉函数

Master the use of auto analyze in data warehouse

TCP两次挥手,你见过吗?那四次握手呢?

Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading

Hough transform Hough transform principle
随机推荐
OpenCV的二值化处理函数threshold()详解
MySQL数据库基本操作-DDL | 黑马程序员
In flinksql, in addition to data statistics, is the saved data itself a state
Educational codeforces round 22 E. Army Creation
FTP, SFTP file transfer
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
Bi skills - permission axis
HDU 6440 2018中国大学生程序设计网络选拔赛
Shell 编程核心技术《一》
偏移量函数及开窗函数
Shell 编程核心技术《四》
长城证券开户安全吗 买股票怎么开户
求2的n次方
HMM隐马尔可夫模型最详细讲解与代码实现
BCG 使用之CBCGPProgressDlg进度条使用
Explicit random number
矩阵翻转(数组模拟)
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
How test engineers "attack the city" (Part 2)
Reflection (I)