Mahdi Javaheri
11/04/2018, 2:13 PMopen class AquariumPlant(val color: String)
class GreenLeafyPlant(size: Int): AquariumPlant("Green")
fun AquariumPlant.print() = println("AquariumPlant")
fun GreenLeafyPlant.print() = println("GreenLeafyPlant")
fun staticExample() {
val plant = GreenLeafyPlant(size = 50)
plant.print() // this print's GreenLeafyPlant
val aquariumPlant: AquariumPlant = plant
aquariumPlant.print() // this print's AquariumPlant !! but in polymorphism should be GreenLeafyPlant
}
is there a way to bypass this and benefit from Polymorphismpdvrieze
11/05/2018, 4:40 PMopen class Parent {
open fun <R> visit(visitor: ParentVisitor<R>): R =
visitor.visitParent(this)
}
class Child1: Parent() {
override fun <R> visit(visitor: ParentVisitor<R>):R =
visitor.visitChild1(this)
}
interface ParentVisitor<R> {
fun visitParent(parent: Parent): R
fun visitChild1(child1: Child1): R
}