小编Wan*_* Li的帖子

如果Golang没有固有的,我该如何尝试构建它

Golang中的struct没有构造函数,我们一般可以使用工厂模式来解决这个问题。但是我怎样才能尝试在工厂模式中实现继承呢?例如,这是我的一些代码:

在student.go中

package model

//a struct
type student struct{
 Name string
 score float64
}

//Because the initial letter of the student structure is lowercase, it can only be used in mods
func NewStudent(n string, s float64) *student {
 return &student{
  Name : n,
  score : s,
 }
}

//If the first letter of the score field is lowercase, then, in other packages not directly method, we can provide a method
func (s *student) GetScore() float64{
 return s.score //ok
}

Run Code Online (Sandbox Code Playgroud)

在main.go中 …

inheritance go

-2
推荐指数
1
解决办法
80
查看次数

标签 统计

go ×1

inheritance ×1