官术网_书友最值得收藏!

Inheritance

There is no inheritance in Go, but you can embed types. Here is an example of a Person and Doctor types, which embeds the Person type. Instead of inheriting the behavior of Person directly, it stores the Person object as a variable, which brings with it all of its expected Person methods and attributes:

package main

import (
"fmt"
"reflect"
)

type Person struct {
Name string
Age int
}
type Doctor struct {
Person Person
Specialization string
}

func main() {
nanodano := Person{
Name: "NanoDano",
Age: 99,
}
drDano := Doctor{
Person: nanodano,
Specialization: "Hacking",
}

fmt.Println(reflect.TypeOf(nanodano))
fmt.Println(nanodano)
   fmt.Println(reflect.TypeOf(drDano))
fmt.Println(drDano)
}
主站蜘蛛池模板: 红安县| 桂东县| 资兴市| 翼城县| 长丰县| 理塘县| 工布江达县| 芜湖市| 滨海县| 莒南县| 惠东县| 石门县| 会理县| 黔南| 衡水市| 沂南县| 福安市| 鄂伦春自治旗| 宁陕县| 淅川县| 吉水县| 土默特左旗| 舒兰市| 枣强县| 建水县| 南雄市| 大田县| 巩留县| 滨州市| 突泉县| 兰坪| 咸宁市| 镶黄旗| 中山市| 汶川县| 东乡县| 蓝田县| 梅河口市| 红河县| 延边| 海安县|