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

  • 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)
}
主站蜘蛛池模板: 罗山县| 化隆| 西城区| 渭源县| 昌平区| 卢龙县| 左云县| 张家界市| 当涂县| 措美县| 蒙阴县| 兴山县| 佛冈县| 阿勒泰市| 五家渠市| 蒙阴县| 宣武区| 泰宁县| 清新县| 千阳县| 土默特右旗| 安乡县| 保山市| 监利县| 六枝特区| 惠安县| 靖宇县| 措勤县| 张家港市| 林芝县| 怀柔区| 临夏市| 昌平区| 新化县| 正镶白旗| 葫芦岛市| 玛曲县| 同江市| 奉节县| 邵武市| 金堂县|