I created a list of classes and sub-classes making up a “Creature” that can be human or alien with multiple appendages and a head.
The class structure is not erroring however, I am unable to access a method think() within the sub-class Human:Creature → Head : Appendage.
Dumping the path I can see that I am able to access the Head class but just not the think() method:
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**
Here’s my list of classes ( sorry for all the code but, it gives context to my problem )
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,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,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,0)
constructor() {}
fun rotateX(degrees: Int)
{
rotation[0] = degrees
}
fun rotateY(degrees: Int)
{
rotation[1] = degrees
}
fun rotateZ(degrees: Int)
{
rotation[2] = degrees
}
}