【发布时间】:2023-03-20 17:27:01
【问题描述】:
考虑下面的例子:我有一个Animal的抽象类,每个动物都有一张嘴,但是因为每个动物的嘴都不一样,所以嘴类也是抽象的:
abstract class Animal {
var numberLegs: Int = 4
var mouth: Mouth? = null
}
abstract class Mouth {
abstract fun makeSound()
}
我现在可以创建一个 Dog 和一个 DogMouth:
class Dog: Animal() {
override var mouth: Mouth = DogMouth()
}
class DogMouth: Mouth() {
override fun makeSound() {
println("Bark!")
}
}
但这也允许我为狗分配其他类型的嘴,这是我不想要的,例如:
class CatMouth: Mouth() {
override fun makeSound() {
println("Meow!")
}
}
fun main() {
val dog = Dog()
dog.mouth.makeSound() // will print "Bark!"
dog.mouth = CatMouth() // I don't want this to work
dog.mouth.makeSound() // will print "Meow!"
}
并且设置override var mouth: DogMouth = DogMouth() 不起作用。
我怎样才能确保狗只有狗嘴(和其他狗的身体部位)?
【问题讨论】:
标签: kotlin polymorphism abstract-class