Go 中从基数 class 到继承 class 的赋值
Assignment from base class to inherit class in Go
我知道 Go 没有这样的 OO 概念,但我借用它们只是为了更容易解释。
OO 继承允许将行为概括为更抽象的类型或 class,使用 class 和它的子 class,子 class 继承所有父级的成员字段和行为 class。 Go 没有这样的概念,但是 can achieve the same paradigm,其中文章解释了 Go 中的 Inheritance 和 subclassing。
我的问题更多是关于作业的。假设我有一辆 "base class" 车辆和一辆 "inherit class" 汽车,如下所示:
type Vehicle struct {
wheelCount int
}
type Car struct {
Vehicle //anonymous field Vehicle
Maker string
}
在真正的 OO 中,我可以将 Vehicle 对象分配给 Car 对象,但我还没有找到在 Go 中执行此操作的方法。所以我的问题是,
- 可以吗?
- 否则,从 "Vehicle" 编写 "Car" 构造函数的 Go 方法是什么?我知道规范的方式是
c := Car{Vehicle{4}, "Ford"}
,但是如果我在 Car 中有其他成员字段,比如 Module 等,我只想初始化 Vehicle 中的成员字段,没有别的怎么办?
我已经为你准备了一些开始的东西,
http://play.golang.org/p/dcs9r7vPjZ
谢谢
I can assign a Vehicle object into a Car object
你是说反面吗?在 OOP 中,您不能将 Vehicle 分配给 Car,但您可以将 Car 或 Van 分配给 Vehicle。
如果是这样,你当然可以在 Go 中这样做:
var v Vehicle
c := Car{Vehicle:Vehicle{1}}
v = c.Vehicle
fmt.Println(v) // prints {1}
此示例还演示了您可以准确地初始化您想要的字段。但是,这种类型的对象构造非常有限,因此构造方法通常用于获得类似构造函数的行为:
func CreateVehicle(wheels int) Vehicle {
return Vehicle{wheels}
}
func CreateCar(wheels int, maker string) Car {
return Car{CreateVehicle(wheels), maker}
}
func main() {
var v Vehicle
c := CreateCar(4, "Ford")
v = c.Vehicle
fmt.Println(v)
}
顺便说一句,如果您通常使用引用类型,那么在 Go 中引用管理可能会很棘手。结构总是按值传递。要通过引用传递它,请使用 &
获取引用并使用 *
指定 byref 参数。
所以,这行不通:
func (v Vehicle) SetNumberOfWeels(wheels int) {
v.wheelCount = wheels
}
func main() {
v := Vehicle{}
v.SetNumberOfWeels(5)
fmt.Println(v) // prints {0}
}
但这将:
func (v *Vehicle) SetNumberOfWeels(wheels int) {
v.wheelCount = wheels
}
func main() {
v := Vehicle{}
v.SetNumberOfWeels(5)
fmt.Println(v) // prints {5}
}
我知道 Go 没有这样的 OO 概念,但我借用它们只是为了更容易解释。
OO 继承允许将行为概括为更抽象的类型或 class,使用 class 和它的子 class,子 class 继承所有父级的成员字段和行为 class。 Go 没有这样的概念,但是 can achieve the same paradigm,其中文章解释了 Go 中的 Inheritance 和 subclassing。
我的问题更多是关于作业的。假设我有一辆 "base class" 车辆和一辆 "inherit class" 汽车,如下所示:
type Vehicle struct {
wheelCount int
}
type Car struct {
Vehicle //anonymous field Vehicle
Maker string
}
在真正的 OO 中,我可以将 Vehicle 对象分配给 Car 对象,但我还没有找到在 Go 中执行此操作的方法。所以我的问题是,
- 可以吗?
- 否则,从 "Vehicle" 编写 "Car" 构造函数的 Go 方法是什么?我知道规范的方式是
c := Car{Vehicle{4}, "Ford"}
,但是如果我在 Car 中有其他成员字段,比如 Module 等,我只想初始化 Vehicle 中的成员字段,没有别的怎么办?
我已经为你准备了一些开始的东西, http://play.golang.org/p/dcs9r7vPjZ
谢谢
I can assign a Vehicle object into a Car object
你是说反面吗?在 OOP 中,您不能将 Vehicle 分配给 Car,但您可以将 Car 或 Van 分配给 Vehicle。
如果是这样,你当然可以在 Go 中这样做:
var v Vehicle
c := Car{Vehicle:Vehicle{1}}
v = c.Vehicle
fmt.Println(v) // prints {1}
此示例还演示了您可以准确地初始化您想要的字段。但是,这种类型的对象构造非常有限,因此构造方法通常用于获得类似构造函数的行为:
func CreateVehicle(wheels int) Vehicle {
return Vehicle{wheels}
}
func CreateCar(wheels int, maker string) Car {
return Car{CreateVehicle(wheels), maker}
}
func main() {
var v Vehicle
c := CreateCar(4, "Ford")
v = c.Vehicle
fmt.Println(v)
}
顺便说一句,如果您通常使用引用类型,那么在 Go 中引用管理可能会很棘手。结构总是按值传递。要通过引用传递它,请使用 &
获取引用并使用 *
指定 byref 参数。
所以,这行不通:
func (v Vehicle) SetNumberOfWeels(wheels int) {
v.wheelCount = wheels
}
func main() {
v := Vehicle{}
v.SetNumberOfWeels(5)
fmt.Println(v) // prints {0}
}
但这将:
func (v *Vehicle) SetNumberOfWeels(wheels int) {
v.wheelCount = wheels
}
func main() {
v := Vehicle{}
v.SetNumberOfWeels(5)
fmt.Println(v) // prints {5}
}