go语言中没有继承的概念,但是可以使用结构体嵌套来模拟继承关系
1.匿名结构体
type TestStructBase struct {
testInt int
}
func (t *TestStructBase) getIntegerValue() int {
return t.testInt
}
type TestStructParent struct {
testInt2 int
TestStructBase
}
func main() {
testStructA := TestStructParent{TestStructBase: TestStructBase{testInt: 1000}}
println((&testStructA).getIntegerValue())
}
2.定义结构体属性
type TestStructBase struct {
testInt int
}
func (t *TestStructBase) getIntegerValue() int {
return t.testInt
}
type TestStructParent struct {
testInt2 int
testStructBase TestStructBase
}
func main() {
testStructA := TestStructParent{testStructBase : TestStructBase{testInt: 1000}}
println((testStructA.testStructBase).getIntegerValue())
}