Я пишу игру в го.В C ++ я храню все свои классы сущностей в массиве класса BaseEntity.Если бы сущности нужно было перемещаться по миру, это был бы PhysEntity, производный от BaseEntity, но с добавленными методами.Я пытался подражать этому go:
package main
type Entity interface {
a() string
}
type PhysEntity interface {
Entity
b() string
}
type BaseEntity struct { }
func (e *BaseEntity) a() string { return "Hello " }
type BasePhysEntity struct { BaseEntity }
func (e *BasePhysEntity) b() string { return " World!" }
func main() {
physEnt := PhysEntity(new(BasePhysEntity))
entity := Entity(physEnt)
print(entity.a())
original := PhysEntity(entity)
// ERROR on line above: cannot convert physEnt (type PhysEntity) to type Entity:
println(original.b())
}
Это не скомпилируется, так как не может сказать, что 'entity' был PhysEntity.Что является подходящей альтернативой этому методу?