子类中的访问方法

我对科特林不满意,并试图创建一个简单的生物等级体系。我似乎已经能够成功完成此操作,但是,我无法访问属于Human:Creature类的孩子的“ Head:Appendage”类中的“ think”方法

我可以看到子类一直打印到head类,但是我无法运行think()方法

val human = Human()
println(human.appendages["Head"])           // [Head@3803641]
println(human.appendages["Head"]?.get(0))   // Head@3803641
human.appendages["Head"]?.get(0).think()    // uncaught reference
import org.omg.CORBA.Object
import java.util.*

open class Creature {
    var appendages = HashMap<String,ArrayList<Appendage>>()
}
open class Appendage {
    constructor() {}
}
open class Human : Creature {
    constructor()
    {
        appendages["Arm"] = ArrayList<Appendage>()
        appendages["Arm"]?.add(Arm())
        appendages["Arm"]?.add(Arm())
        appendages["Leg"] = ArrayList<Appendage>()
        appendages["Leg"]?.add(Leg())
        appendages["Leg"]?.add(Leg())
        appendages["Head"] = ArrayList<Appendage>()
        appendages["Head"]?.add(Head("Human"))
    }
}
class Alien : Creature
{
    constructor()
    {
        appendages["Tentacle"] = ArrayList<Appendage>()
        appendages["Tentacle"]?.add(Tentacle())
        appendages["Tentacle"]?.add(Tentacle())
        appendages["Tentacle"]?.add(Tentacle())
        appendages["Tentacle"]?.add(Tentacle())
        appendages["Arm"] = ArrayList<Appendage>()
        appendages["Arm"]?.add(Arm())
        appendages["Arm"]?.add(Arm())
        appendages["Leg"] = ArrayList<Appendage>()
        appendages["Leg"]?.add(Leg())
        appendages["Leg"]?.add(Leg())
        appendages["Head"] = ArrayList<Appendage>()
        appendages["Head"]?.add(Head("Alien"))
        appendages["Head"]?.add(Head("Alien"))
    }
}

class Head : Appendage {
    private var whatami : String = ""
    constructor(whatami : String)
    {
        this.whatami = whatami
    }
    public fun think()
    {
        println("$whatami Thinking")
    }
}
open class Arm : Appendage {
    private var rotation = mutableListOf<Int>(0,0)
    constructor() {}
    fun rotateX(degrees: Int)
    {
        rotation[0] = degrees
    }
    fun rotateY(degrees: Int)
    {
        rotation[1] = degrees
    }
    fun rotateZ(degrees: Int)
    {
        rotation[2] = degrees
    }
}
open class Leg : Appendage {
    private var rotation = mutableListOf<Int>(0,0)
    constructor() {}
    fun rotateX(degrees: Int)
    {
        rotation[0] = degrees
    }
    fun rotateY(degrees: Int)
    {
        rotation[1] = degrees
    }
    fun rotateZ(degrees: Int)
    {
        rotation[2] = degrees
    }
}
open class Tentacle : Appendage {
    private var rotation = mutableListOf<Int>(0,0)
    constructor() {}
    fun rotateX(degrees: Int)
    {
        rotation[0] = degrees
    }
    fun rotateY(degrees: Int)
    {
        rotation[1] = degrees
    }
    fun rotateZ(degrees: Int)
    {
        rotation[2] = degrees
    }
}

我希望程序打印出“人类思维”,但出现“未引用”错误

qq492096437 回答:子类中的访问方法

正如Tom所说,您的编译器对类型一无所知。您需要进行如下简单的转换:

(human.appendages["Head"]?.get(0) as Head).think()
本文链接:https://www.f2er.com/3163240.html

大家都在问