从下面的两段代码中可以非常直观的看出,Go中的继承跟C++相比较而言是非常方便的
单重继承
package main
import "fmt"
//---------------单重继承----------------
type Car struct {
weight int
name string
}
func (p *Car) Run() {
fmt.Println("running")
}
type Bike struct {
Car
lunzi int
}
func main() {
var a Bike
a.weight = 100
a.name = "bike"
a.lunzi = 2
fmt.Println(a)
a.Run()
}
多重继承
package main
import "fmt"
//----------------多重继承----------------
type Cart1 struct {
name string
age int
}
type Cart2 struct {
name string
age int
}
type train struct {
Cart1
Cart2
}
func main() {
var t train
t.Cart1.name = "train"
t.Cart1.age = 100
fmt.Println(t)
}