- 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)
}
推薦閱讀
- 感官的盛宴:數(shù)學(xué)之眼看藝術(shù)(萬物皆數(shù)學(xué))
- 線性代數(shù)
- 線性代數(shù)及其應(yīng)用(原書第6版)
- Origin 9.0科技繪圖與數(shù)據(jù)分析超級學(xué)習(xí)手冊
- 數(shù)學(xué)建模33講:數(shù)學(xué)與繽紛的世界
- Blockchain for Decision Makers
- 數(shù)學(xué)原來可以這樣學(xué):初中篇
- 10堂極簡概率課
- 怎樣解題:數(shù)學(xué)競賽攻關(guān)寶典(第3版)
- ANSYS Workbench 2020有限元分析從入門到精通(升級版)
- 數(shù)學(xué)女孩5:伽羅瓦理論
- 數(shù)學(xué)建模
- 現(xiàn)代啟發(fā)式優(yōu)化方法及其應(yīng)用
- 迷人的數(shù)學(xué)+美麗的數(shù)學(xué)(共2冊)
- 概率論與數(shù)理統(tǒng)計