package main import "fmt" type A struct { animal Animal } type Animal struct { Name string } func (a Animal) Move() { fmt.Printf("%s is moving\n", a.Name) } // 现在我想扩展一下 animal 的 Move 方法 type Dog struct { Animal // 嵌入结构体 Breed string } func (d Dog) Move() { fmt.Printf("Dog is Move") } func main() { dog := Dog{ Animal: Animal{Name: "Buddy"}, Breed: "Golden Retriever", } // 报错 a.animal = dog }
实际工作中还是有这样的场景的,某个 class/struct 里面引用了一个外部的 class/struct 。我想对他调用的某个函数进行扩展一下,Java 里面,我只要继承这个外部的类,然后 override 一下我需要改写的方法,然后再改写一下赋值语句就可以。但是 golang 中好像不行,必须把类型也改了才能赋值。
当然了,如果这个变量类型是个 interface 的话,倒是可以的,但是现实情况中遇到的就是个 struct 。
![]() | 1 Biem 348 天前 了解一下 go 中`interface`的概念 ```go package main import "fmt" // 定义一个 Animal 接口 type AnimalInterface interface { Move() } // 定义 Animal 结构体 type Animal struct { Name string } func (a Animal) Move() { fmt.Printf("%s is moving\n", a.Name) } // 定义 Dog 结构体,嵌入 Animal type Dog struct { Animal // 这里嵌入了 Animal Breed string } // Dog 实现 AnimalInterface 接口 func (d Dog) Move() { fmt.Printf("Dog %s is moving\n", d.Name) } type A struct { animal AnimalInterface } func main() { dog := Dog{ Animal: Animal{Name: "Buddy"}, Breed: "Golden Retriever", } // 赋值给 A 中的 animal 字段 a := A{animal: dog} // 调用 Move 方法 a.animal.Move() // 输出: Dog Buddy is moving } ``` |
![]() | 2 laikicka 348 天前 ![]() 可以使用接口来定义行为,然后让结构体实现该接口. golang 的思想和 java 不一样 |
3 crackidz 348 天前 首先,Go 里没有类... |
![]() | 4 wh1012023498 348 天前 ``` package main import "fmt" type Animal struct { Name string } func (a Animal) Move() { fmt.Printf("%s is moving\n", a.Name) } // 现在我想扩展一下 animal 的 Move 方法 type Dog struct { *Animal // 嵌入结构体 Breed string } func (d Dog) Move() { fmt.Printf("Dog is Move") } func main() { dog := Dog{ Animal: &Animal{Name: "Buddy"}, Breed: "Golden Retriever", } // 不会报错 dog.Move() } ``` |
![]() | 5 wh1012023498 348 天前 理解错了,还要赋值给一个明确类型的结构体变量,那必然要用 interface 了。 |
6 NessajCN 348 天前 go 里没有类,更没有封装,没有继承,没有子类父类等等所有 jvav 里的糟粕 你要做的只是定义个函数然后调用就好了。struct 只是个数据结构不是 class |
7 james122333 348 天前 via Android Go 没有类只有数据结构 没有继承只有组合 组合优于继承 但 go 的组合更好 |
8 james122333 348 天前 via Android 在 java 里除了变量可视或称封装以外还有继承阻挡你动态的应用自己或别人写的东西 go 里就变量可视最麻烦 因应业务将架构调整成适合的样子 go 方便很多 动态性差的东西解决需求麻烦非常多 |
![]() | 9 xuanbg 347 天前 ![]() |
![]() | 10 Edsie 347 天前 拉踩 java 能体现优越感 |
12 rainbowStay 347 天前 问题还是在与 golang 没有真正的"继承"概念,也就没有针对父子类的多态,因此不能方法重写 |
13 james122333 347 天前 via Android |
![]() | 14 xuanbg 347 天前 @InkStone @james122333 谁也没强迫 Javaer 去滥用继承啊。一个人的水平不行怎么能怪语言设计的不好呢?虽然 Java 语言也确实有不少槽点,但继承真的不是。继承就是封装的一种语言级别的体现而已。 |
![]() | 15 grzhan 346 天前 其实 Golang 的 Embedding (嵌入) 也很灵活,因为嵌入不光能够嵌入 struct ,还能嵌入 interface 。 经典的实现是标准库 context ,比如实现 context.WithCancel 的关键结构体 cancelCtx ,就是嵌入了接口 Context ,当 cancelCtx 初始化时,会把 parent 塞给 cancelCtx.Context ,关键在于由于 cancelCtx.Context 是个接口,所以你可以把任意实现了 Context 接口的类型作为 parent 塞给 cancelCtx ,以此实现一种“继承”。 cancelCtx 源码: https://github.com/golang/go/blob/76f3e0ac8d094b2bc5f8a3fb8a19d1d17a07fe2c/src/context/context.go#L423 这就是为什么不同的 context 底层结构体( cancelCtx 、timerCtx 、valueCtx……)可以通过 WithCancel 、WithDeadline 、WithValue 等标准库方法组成一个灵活的“context 链”, 还能够基于拼接顺序 "override" 各自的实现方法,第一次看源码的时候觉得还是挺奇妙的。 所以当你有扩展行为实现的需求的时候,在 Go 确实要首先考虑用接口 |
16 james122333 346 天前 via Android |