假设我有两只宠物,一只叫露西的猫和一只叫菲多的狗。我教了他们两个相同的技巧,“说话”。
以后想养更多的宠物,教给它们不同的把戏,所以心存期待,写了一个接口(interface):
package main
import "fmt"
type Pet interface {
speak() string
}
type Dog struct {
speech string
}
type Cat struct {
speech string
}
func (c Cat) speak() string {
return c.speech
}
func (d Dog) speak() string {
return d.speech
}
func getSpeech(p Pet) string {
return p.speak()
}
func main() {
Fido := Dog{"woof"}
Lucy := Cat{"meow"}
fmt.Println("Fido says:", getSpeech(Fido)) // Fido says: woof
fmt.Println("Lucy says:", getSpeech(Lucy)) // Lucy says: meow
}
现在,虽然这很好用,但似乎不必要地冗长。我清楚地重复自己。另外,假设所有的狗都说“woof”,所有的猫都说“meow”,在结构中初始化字符串是惯用的吗?
您将如何在不失去接口(interface)优势的情况下重构此代码以使其更加 DRY?
最佳答案
在某些情况下,您可以嵌入一个基类型来委托(delegate)公共(public)字段和方法。这不是继承,它只是一种通过组合自动委托(delegate)的形式。不要像在 java 风格的 OOP 语言中那样尝试使用它来创建类型层次结构。
在这里您可以将speak
方法委托(delegate)给Speaker
类型。在实践中,这不太有用,因为 Speaker
及其方法与它们嵌入的结构没有关系,即 Speaker 不知道它在为哪种类型说话,也不知道哪个实例。
https://play.golang.org/p/Bof92jZsNh
type Speaker struct {
Saying string
}
func (s Speaker) speak() string {
return s.Saying
}
type Pet interface {
speak() string
}
type Dog struct {
Speaker
}
type Cat struct {
Speaker
}
func getSpeech(p Pet) string {
return p.speak()
}
func main() {
Fido := Dog{Speaker{Saying: "woof"}}
Lucy := Cat{Speaker{Saying: "meow"}}
fmt.Println("Fido says:", getSpeech(Fido)) // Fido says: woof
fmt.Println("Lucy says:", getSpeech(Lucy)) // Lucy says: meow
}
关于go - 在 Go 中编写接口(interface)而不重复自己,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38014647/