How to simiuate super.init() in Python
type Baser interface {
Init(name string)
Do()
}
type BaseClass struct {
name string
}
func (cls *BaseClass) Init(name string) {
cls.name = name
}
type SepcificClass struct {
BaseClass
}
func (cls *SpecificClass) Init(name string) {
cls.BaseClass.Init(name)
}
func main() {
var cls *specificClass
cls = &SpecificClass{}
cls.Init("ryan")
}
How to automacially init base class’s instance value
type Baser interface {
Do()
}
type BaseClass struct {
name string
}
func (cls *BaseClass) Do() {
}
type SepcificClass struct {
BaseClass
}
func (cls *SepcificClass) Do() {
}
// the explict way to initialize base class's value
func (cls *SpecificClass) New1() Baser {
return &SepcificClass{
BaseClass{"ryan"}
}
}
func (cls *SpecificClass) New2() Baser {
instance := &SepcificClass{}
instance.Baseclass.name = "ryan"
}
// the implicit way by using reflect
func (cls *SpecificClass) New2() Baser {
t := reflect.TypeOf(&SpecificClass{}).Elem()
v := t.New().Elem()
v.FieldByName("name").setString("ryan")
}