继承
This commit is contained in:
parent
d5703bfb50
commit
7d08744109
24
src/class/Animal.kt
Normal file
24
src/class/Animal.kt
Normal file
@ -0,0 +1,24 @@
|
||||
package `class`
|
||||
|
||||
/**
|
||||
* 继承
|
||||
*
|
||||
*
|
||||
*/
|
||||
open class Animal(name: String, color: String, age: Int) {
|
||||
|
||||
private var name: String
|
||||
private var color: String
|
||||
private var age: Int = 0
|
||||
|
||||
// 初始化
|
||||
init {
|
||||
this.name = name
|
||||
this.color = color
|
||||
this.age = age
|
||||
}
|
||||
|
||||
open fun say(): Unit {
|
||||
println("name = $name,color = $color,age = $age")
|
||||
}
|
||||
}
|
||||
12
src/class/Cat.kt
Normal file
12
src/class/Cat.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package `class`
|
||||
|
||||
/**
|
||||
* 继承
|
||||
*
|
||||
*/
|
||||
class Cat(name: String, color: String, age: Int) : Animal(name, color, age) {
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Cat("小猫","橘色",3).say()
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user