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

  • 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)
}
主站蜘蛛池模板: 宁海县| 佛山市| 蒙阴县| 洪泽县| 平利县| 谷城县| 时尚| 华池县| 九龙坡区| 徐州市| 彭阳县| 赣州市| 广元市| 淳安县| 平谷区| 蓬莱市| 红原县| 子洲县| 台东市| 寿阳县| 道孚县| 汨罗市| 新宁县| 东明县| 界首市| 宁德市| 建德市| 收藏| 买车| 耿马| 岚皋县| 庆元县| 南昌市| 隆尧县| 石棉县| 灵石县| 汨罗市| 涿州市| 当阳市| 黑山县| 蕉岭县|