关于go:Golang-实现继承

2次阅读

共计 360 个字符,预计需要花费 1 分钟才能阅读完成。

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)
}
正文完
 0