GOLANG学习26. 结构体继承
package main
import (
"fmt"
)
//定义父类
type Car struct {
Name string
Color string
}
//父类的run 方法
func (p Car) run() {
fmt.Printf("%s is running\n", p.Name)
}
//子类继承父类,通过匿名字段继承方式
type Bike struct {
Car
wheel int
}
//子类继承父类,通过匿名字段继承方式
type Train struct {
Car
seats int
}
func main() {
var b Bike
b.Name = "Bike"
b.run()
var t Train
t.Name = "Train"
t.seats = 1800
t.run()
}