seo如何优化,优化大师安卓版,wordpress自定义二级菜单的标签,店铺推广策略继承
Go 语言的设计之初#xff0c;就不打算支持面向对象的编程特性#xff0c;因此 Go 不支持面向对象的三大特性之一——继承。但是 Go 可以通过组合的思想去实现 “继承”。继承是面向对象的三大特性之一#xff0c;继承是从已有的类中派生出新的类#xff0c;新的类能…继承
Go 语言的设计之初就不打算支持面向对象的编程特性因此 Go 不支持面向对象的三大特性之一——继承。但是 Go 可以通过组合的思想去实现 “继承”。继承是面向对象的三大特性之一继承是从已有的类中派生出新的类新的类能吸收已有类的数据属性和行为并能扩展新的能力。Go 语言里的“继承”体现如一个结构体拥有另一个结构体的的所有字段和方法并在此基础上定义新的字段和方法。
代码
import fmttype Person struct {Name stringAge int
}func (p Person) Introduce() {fmt.Printf(大家好我叫%s,我今年%d岁了。\n, p.Name, p.Age)
}type Student struct {PersonSchool string
}func (s Student) GoToTheClass() {fmt.Println(去上课...)
}func main() {student : Student{}student.Name 小明student.Age 18student.School 太阳系大学// 执行 Person 类型的 Introduce 方法student.Introduce()// 执行自身的 GoToTheClass 方法student.GoToTheClass()
}输出
大家好我叫小明,我今年18岁了。
去上课...接口 直接看代码
package main
import (fmt
)//Monkey结构体
type Monkey struct {Name string
}//声明接口
type BirdAble interface {Flying()
}type FishAble interface {Swimming()
}func (this *Monkey) climbing() {fmt.Println(this.Name, 生来会爬树..)
}//LittleMonkey结构体
type LittleMonkey struct {Monkey //继承
}//让LittleMonkey实现BirdAble
func (this *LittleMonkey) Flying() {fmt.Println(this.Name, 通过学习会飞翔...)
}//让LittleMonkey实现FishAble
func (this *LittleMonkey) Swimming() {fmt.Println(this.Name, 通过学习会游泳..)
}func main() {//创建一个LittleMonkey 实例monkey : LittleMonkey{Monkey {Name : 悟空,},}monkey.climbing()monkey.Flying()monkey.Swimming()}输出
悟空 生来会爬树..
悟空 通过学习会飞翔...
悟空 通过学习会游泳..总结
当A结构体继承了B结构体那么A结构体就自动的继承了B结构体的字段和方法并且可以直接使用。
当A结构需要扩展功能同时不希望去破坏继承关系则可以去实现某个接口即可因此我们可以认为实现接口是对继承机制的补充。