类和接口
类的定义
class Simple
class Simple2 {
var index: Int = 0
fun action() {}
}
class Simple3 {
var index: Int
constructor(x: Int) {
this.index = x
}
}
class Simple4 {
constructor() {}
constructor(x: Int) {}
constructor(x: Int, y: String) {}
}
class Simple5 constructor(x: Int) {
var index:Int = x
constructor(x: Int, y: String) : this(x) {}
constructor(x: Int, y: String, z: Int) : this(x, y) {}
}
class Simple6(x: Int) {
var index: Int = x
constructor(x: Int, y: String) : this(x) {}
constructor(x: Int, y: String, z: Int) : this(x, y) {}
}
class Simple7(var index: Int) {
constructor(x: Int, y: String) : this(x) {}
constructor(x: Int, y: String, z: Int) : this(x, y) {}
}
接口的定义
interface SimpleInf{
fun action()
private fun action2(){
println("Hello")
}
}
接口的实现
interface SimpleInf {
fun action()
}
class SimpleClass : SimpleInf {
override fun action() {
}
}
抽象类定义
abstract class AbsClass {
abstract fun action1()
open fun action2() {}
final fun action3() {}
}
class SimpleClass: AbsClass() {
override fun action1() {
}
override fun action2() {
super.action2()
}
}
类的继承
open class SuperClass {
fun action1(){}
open fun action2(){}
}
class SimpleClass: SuperClass() {
var x = action1()
override fun action2() {
super.action2()
}
}
类的属性
class Student(age: Int, name: String) {
var age: Int = age
get() {
println("Hello")
return field
}
set(value) {
field = value
println("Hello")
}
var name: String = name
}
fun action(){
val ageRef: (Student) -> Int = Student::age
val ageRef2: Function1<Student, Int> = Student::age
val student: Student = Student(18, "Nemo")
val nameRef: () -> String = student::name
val nameRef2: Function0<String> = student::name
}