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

  • Security with Go
  • John Daniel Leon
  • 106字
  • 2021-06-30 19:06:50

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)
}
主站蜘蛛池模板: 武定县| 杂多县| 息烽县| 晋中市| 福鼎市| 岳普湖县| 上饶市| 彰化县| 龙游县| 拉萨市| 容城县| 东光县| 固始县| 富锦市| 镇沅| 西青区| 鄱阳县| 青龙| 永春县| 焉耆| 商都县| 平泉县| 东海县| 堆龙德庆县| 祁阳县| 麻栗坡县| 榆林市| 康定县| 甘泉县| 彭州市| 特克斯县| 杭锦后旗| 临高县| 常宁市| 万荣县| 荣昌县| 化隆| 怀远县| 台前县| 织金县| 巨鹿县|