如果你想象我有以下声明:
type Car struct {
Vehicle
engineType string
}
type Bus struct {
Vehicle
public bool
engineType string
}
type Bike struct {
Vehicle
motorbike bool
}
type Vehicle struct {
NumberWheels int
NumberPassengers int
Owner string
}
type Vehicles []Vehicle
我正在尝试拥有一系列车辆。然而,这是不可能的,因为他们每个人都有不同的类型(即Car,Bus,Bike,等...)
var myCar = Car{Vehicle{4, 4, "Me"}, "Manual"}
var myBike = Bike{Vehicle{2, 0, "Bob and I"}, false}
var myVehicles = Vehicles{myCar, myBike}
for i := range myVehicles {
fmt.Println(myVehicles[i])
}
你将如何实现这样的事情。还是我试图从错误的角度解决这个问题。我是 Go 的新手
侃侃尔雅
相关分类