是否可以在没有对象初始化程序的情况下进行继承?
type BaseEmployee struct{
LastName string
}
type Employee struct {
Name string,
BaseEmployee
}
现在我有这个:
ob := Employee {
FirstName: "Ron"
LastName : BaseEmployee {LastName : "Howard"}
}
但是我想要实现以下目标:
ob := Employee {
FirstName: "Ron"
LastName : "Howard",//<<<<<<<<<---- how us that possible?
}
可能吗?
答案 0 :(得分:1)
根据您对问题的评论,无法将结构字段“隐式”链接到两个不同的结构。
如果您要反对的是结构初始化语法,则可以通过编写便利函数来初始化新的BaseEmployee来解决此问题,例如:
func NewBaseEmployee(name string) BaseEmployee {
return BaseEmployee{LastName: name}
}
ob := Employee {
FirstName: "Ron"
LastName : NewBaseEmployee("Howard")
}