Go 结构体
由于go没有class类, 很多语法类似C,继承使用了C的结构体
struct
但是我看到go
结构体里面,一般都是定义变量属性,没看到定义方法的,
那么如何实现构造方法了?
实现构造方法
在其他语言里面,一般实例化对象,都有会构造方法,Go里面一般是这样使用,间接实现构造方法,返回一个结构体对象。
package main
import (
"fmt"
)
// 在定义一个结构体
type Mouse struct {
name string
color string
sex bool
}
// 模拟构造方法 方法名随意
func NewMouse(name string, color string, sex bool) (m Mouse) {
// 返回结构体对象 间接实现构造方法
return Mouse{
name:name,
color: color,
sex: sex,
}
}
func main() {
// 构造方法实例化
Tuffy := NewMouse("Tuffy", "grey", true)
fmt.Println(Tuffy)
}
结构体
package main
import (
"fmt"
)
// 声明一个结构体
type Cat struct {
name string
color string
sex bool
couple Mouse // 这里赋值为 另一个结构体, 如果和结构体名一样 可以省略一个
}
// 结构体挂载方法
func (c *Cat) CaptureMouse(name string) (res bool) {
if c.name == name {
fmt.Println("名字为Tom,能捉到老鼠")
// 由于使用了指针, 可以改变结构体的值
c.name = "old tom "
return true
} else {
fmt.Println("名字不为Tom, 捉不到老鼠")
return false
}
}
// 在定义一个结构体
type Mouse struct {
name string
color string
sex bool
}
// 结构体挂载方法 不使用指针
func (m Mouse) Eat(stuff string) (res bool) {
if stuff == "奶酪" {
fmt.Println(m.name, "喜欢吃", stuff)
// 不能改变外部结构体的值
m.name = "old jerry"
return true
} else {
fmt.Println(m.name, "不喜欢吃", stuff)
return false
}
}
func main() {
// 实例化结构体
Tom := Cat{
name: "Tom",
color: "white",
sex: true,
couple: Mouse{
name: "Jerry",
},
}
// 调用方法
res := Tom.CaptureMouse("Tom")
fmt.Println(res)
fmt.Println(Tom)
// 通过子类属性调用子类的方法(我自己取的名字)
Tom.couple.Eat("糖")
Tom.couple.Eat("奶酪")
}
总结
感觉go
语言很大程度上继承了C语言,我内心一直想把结构体当成类,去使用,但是老感觉不舒服,好别扭。