在Go中从基类分配给继承类

I know Go doesn't have such OO concept, but let me borrow them just to make it easier to explain.

OO inherits allows generalizing behavior to a more abstract type or class, using a class and a subclass of it, where the subclass inherits all the member fields and behaviors of the parent class. Go doesn't have such concept, but can achieve the same paradigm, in which the article explains Inheritance and subclassing in Go.

My question is more on the assignment. Suppose I have a "base class" Vehicle and a "inherit class" Car, like the following:

type Vehicle struct {
    wheelCount int
}

type Car struct {
    Vehicle //anonymous field Vehicle
    Maker   string
}

In true OO, I can assign a Vehicle object into a Car object, but I haven't found a way to do that in Go. So my questions are,

  • Is it possible to do that?
  • Else, what's the Go way to write a "Car" constructor from a "Vehicle"? I know the canonical way is c := Car{Vehicle{4}, "Ford"}, but what if I have other member fields in Car, like Module etc, and I'd like only to initial the member fields from Vehicle, nothing else?

I've prepared something for you to start with, http://play.golang.org/p/dcs9r7vPjZ

Thanks

I can assign a Vehicle object into a Car object

Do you mean opposite? In OOP you cannot assign Vehicle to Car, but you can assign either Car or Van to Vehicle.

If so, you can do this in Go, of course:

var v Vehicle
c := Car{Vehicle:Vehicle{1}}
v = c.Vehicle
fmt.Println(v) // prints {1}

This example also demonstrates that you can initialise exactly the fields you want to. However, this type of object construction is quite limited so the construction method is what typically used to get constructor-like behaviour:

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)
}

BTW, the reference management can be tricky in Go if you normally work with reference types. The structs are always passed by value. To pass it by reference, use & to get reference and * to specify the byref parameter.

So, this will not work:

func (v Vehicle) SetNumberOfWeels(wheels int) {
    v.wheelCount = wheels
}

func main() {
    v := Vehicle{}
    v.SetNumberOfWeels(5)
    fmt.Println(v) // prints {0}
}

But this will:

func (v *Vehicle) SetNumberOfWeels(wheels int) {
    v.wheelCount = wheels
}

func main() {
    v := Vehicle{}
    v.SetNumberOfWeels(5)
    fmt.Println(v) // prints {5}
}