Go 继承
在面向对象编程(OOP)中,继承是一种机制,允许一个类(子类)从另一个类(父类)继承属性和方法。通过继承,子类可以复用父类的代码,并且可以在不修改父类的情况下扩展或修改其行为。
Go 语言并不是一种传统的面向对象编程语言,它没有类和继承的概念。
Go 使用结构体(struct)和接口(interface)来实现类似的功能。
Go 中的 "继承"
Go 语言没有传统面向对象语言中的类(class)和继承(inheritance)概念,而是通过组合(composition)和接口(interface)来实现类似的功能。
1. 组合(Composition)
组合是 Go 中实现代码复用的主要方式。通过将一个结构体嵌入到另一个结构体中,子结构体可以"继承"父结构体的字段和方法。
实例
package main
import "fmt"
// 父结构体
type Animal struct {
Name string
}
// 父结构体的方法
func (a *Animal) Speak() {
fmt.Println(a.Name, "says hello!")
}
// 子结构体
type Dog struct {
Animal // 嵌入 Animal 结构体
Breed string
}
func main() {
dog := Dog{
Animal: Animal{Name: "Buddy"},
Breed: "Golden Retriever",
}
dog.Speak() // 调用父结构体的方法
fmt.Println("Breed:", dog.Breed)
}
import "fmt"
// 父结构体
type Animal struct {
Name string
}
// 父结构体的方法
func (a *Animal) Speak() {
fmt.Println(a.Name, "says hello!")
}
// 子结构体
type Dog struct {
Animal // 嵌入 Animal 结构体
Breed string
}
func main() {
dog := Dog{
Animal: Animal{Name: "Buddy"},
Breed: "Golden Retriever",
}
dog.Speak() // 调用父结构体的方法
fmt.Println("Breed:", dog.Breed)
}
代码解释
Animal
是父结构体,包含一个字段Name
和一个方法Speak
。Dog
是子结构体,通过嵌入Animal
结构体,继承了Animal
的字段和方法。- 在
main
函数中,我们创建了一个Dog
实例,并调用了Speak
方法。
2. 接口(Interface)
接口是 Go 中实现多态的主要方式。通过定义接口,不同的结构体可以实现相同的方法,从而实现类似继承的多态行为。
示例代码
实例
package main
import "fmt"
// 定义接口
type Speaker interface {
Speak()
}
// 父结构体
type Animal struct {
Name string
}
// 实现接口方法
func (a *Animal) Speak() {
fmt.Println(a.Name, "says hello!")
}
// 子结构体
type Dog struct {
Animal
Breed string
}
func main() {
var speaker Speaker
dog := Dog{
Animal: Animal{Name: "Buddy"},
Breed: "Golden Retriever",
}
speaker = &dog
speaker.Speak() // 通过接口调用方法
}
import "fmt"
// 定义接口
type Speaker interface {
Speak()
}
// 父结构体
type Animal struct {
Name string
}
// 实现接口方法
func (a *Animal) Speak() {
fmt.Println(a.Name, "says hello!")
}
// 子结构体
type Dog struct {
Animal
Breed string
}
func main() {
var speaker Speaker
dog := Dog{
Animal: Animal{Name: "Buddy"},
Breed: "Golden Retriever",
}
speaker = &dog
speaker.Speak() // 通过接口调用方法
}
代码解释
Speaker
是一个接口,定义了一个Speak
方法。Animal
结构体实现了Speaker
接口。Dog
结构体通过嵌入Animal
结构体,间接实现了Speaker
接口。- 在
main
函数中,我们将Dog
实例赋值给Speaker
接口,并通过接口调用Speak
方法。
Go 与经典继承的区别
特性 | 经典继承 | Go 的方式 |
---|---|---|
代码复用 | 通过继承 | 通过组合(嵌入结构体) |
多态 | 通过继承和方法重写 | 通过接口实现 |
关系 | "是一个"(is-a)关系 | "有一个"(has-a)或"实现了"关系 |
灵活性 | 继承关系固定 | 可以运行时组合 |
完整继承模拟:
实例
package main
import "fmt"
// 基类
type Vehicle struct {
Brand string
}
func (v *Vehicle) Start() {
fmt.Println(v.Brand, "started")
}
// 派生类
type Car struct {
Vehicle // 嵌入Vehicle
Model string
}
// 重写Start方法
func (c *Car) Start() {
fmt.Println(c.Brand, c.Model, "car started")
}
func main() {
v := Vehicle{Brand: "Toyota"}
c := Car{
Vehicle: Vehicle{Brand: "Honda"},
Model: "Civic",
}
v.Start() // Toyota started
c.Start() // Honda Civic car started
c.Vehicle.Start() // Honda started
}
import "fmt"
// 基类
type Vehicle struct {
Brand string
}
func (v *Vehicle) Start() {
fmt.Println(v.Brand, "started")
}
// 派生类
type Car struct {
Vehicle // 嵌入Vehicle
Model string
}
// 重写Start方法
func (c *Car) Start() {
fmt.Println(c.Brand, c.Model, "car started")
}
func main() {
v := Vehicle{Brand: "Toyota"}
c := Car{
Vehicle: Vehicle{Brand: "Honda"},
Model: "Civic",
}
v.Start() // Toyota started
c.Start() // Honda Civic car started
c.Vehicle.Start() // Honda started
}
Go 的这种设计避免了传统继承的许多问题,如脆弱的基类问题,同时提供了更大的灵活性。
点我分享笔记