package main
// 父结构体
type BaseController struct {
}
// 父结构体的方法
func (c *BaseController) ReturnJson() {
}
// 继承
type UserController struct {
BaseController
}
// 组合
type OtherController struct {
userController UserController
}
func main() {
// 调用
user := UserController{}
other := OtherController{}
user.ReturnJson() //继承: 直接调用父结构体方法
other.userController.ReturnJson() //组合: 调用结构体方法
other.ReturnJson() //组合: 此处报错,不能直接调用父结构体方法
}
Go语言中的继承与组合
最新推荐文章于 2024-01-16 19:53:49 发布