关于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)
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理