乐趣区

关于go: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)
}
退出移动版