Golang 实现继承

Golang的继承可以通过结构体里面包含匿名结构体实现,具体,比如iPhone这个结构体要继承法phone这个结构体可以这样写:

package main

import "fmt"

type phone struct {
    design_place     string
    production_place string
}

type iphone struct {
    brand string
    phone
}

func main() {
    thePhone := phone{
        design_place:     "California",
        production_place: "China",
    }
    thisPhone := iphone{
        brand: "Apple",
        phone: thePhone,
    }

    fmt.Println(thisPhone.production_place, thisPhone.brand)
}

评论

暂无评论