Go笔记-面向对象初阶
Suriski 3/16/2022 golang笔记
# 面向对象解释
面向对象是软件开发的方法
泛指现实中一切实体失误,每种事物都具备自己的属性和行为
将事物的属性、行为抽象出来,描述成计算机事件的设计思想,
# 抽象比喻
类型组合,在一个类型当中嵌入一个或多个类型来实现面向对象
go class struct 抽象的试题(类)与对象 结构体的与结构变量 接口(规范)来增加实体的方法
# 面向对象三大特性
# 封装
对外暴露公开的接口、隐藏对内的细节。
增强安全,简化编程
practice_oop
type Product struct {
productName string
productPrice float32
}
//getter setter
func (this *Product) SetProductName(_productName string) {
this.productName = _productName
}
func (this *Product) GetProductName() string {
return this.productName
}
func (this *Product) SetProductPrice(_productPrice float32) {
this.productPrice = _productPrice
}
func (this *Product) GetProductPrice() float32 {
return this.productPrice
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
main
func main(){
product := &practice_oop.Product{}
product.SetProductName("Jira")
fmt.Println(product.GetProductName())
}
1
2
3
4
5
2
3
4
5
# 继承
通俗:王思聪继承老爸王健林的亿万家产
子类继承父类,自动拥有父类的属性和方法
payment.go
type PaymentArgs struct {
AppID string
MchID string
Key string
CallbackUrl string
}
func (this *PaymentArgs)Info() {
fmt.Println("paymentArgs = %v\n",this)
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
payment_AliPay.go
type AliPay struct {
PaymentArgs //继承PaymentArgs,匿名结构图
PaymentOther PaymentOther //继承PaymentOther,有名结构体
AliPayID string
}
func (this *AliPay)Info() {
fmt.Println("123132")
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
payment_Other.go
type PaymentOther struct {
AppID string
MchID string
Key string
CallbackUrl string
}
1
2
3
4
5
6
2
3
4
5
6
payment_Wechat.go
type WeixinPay struct {
PaymentArgs //继承PaymentArgs
WeixinOpenID string
}
1
2
3
4
2
3
4
main.go:
func main(){
alipay := &practice_oop.AliPay{
PaymentArgs: practice_oop.PaymentArgs{
AppID: "ali1234appid",
MchID: "ali1234Mchid",
Key: "ali1234key",
},
AliPayID: "alipayId",
}
alipay.PaymentOther.AppID = "paymentOther.appid"
alipay.Info() //重写父类方法
fmt.Println(alipay.PaymentArgs.AppID)
fmt.Println(alipay.PaymentOther.AppID)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
优点:
- 提高代码复用率
- 提高代码的拓展性和维护性
访问流程:
- 先判断APPID 是否属于Alipay
- 如果没有,继续去找他继承的结构体,有就访问
- 如果没有,继续去找PaymentArgs这个结构体锁继承的结构体,如果有就访问,么有则报错
- 如果一个结构体中继承了多个结构体,而这些多结构体当中有相同的字段,那么我们就需要使用结构体的名来访问。
- 一个内置的类型可以作为结构体的匿名字段,这种方式只能在本包访问
# 多态
通俗:水具有多种心态,气体(水蒸气)、液体(水)、固体(冰)
通过接口来实现
继承、重写、父类应用指向子类对象
type 接口名 interface {
方法名1(参数列表)(返回值)
方法名2(参数列表)(返回值)
方法名3(参数列表)(返回值)
}
1
2
3
4
5
2
3
4
5
接口说明:
定义接口时,方法不能实现 一个类型如果实现了接口中所有方法,我们就说这种类型实现接口,不仅仅结构体,也可以使内置类型
自定义类似,可以实现多个接口
//自定义类型,基于内置类型
type readwrite string
type write interface {
echo()
out()
}
type read interface {
scan()
input()
}
func (this *readwrite)echo() {
fmt.Println("echo()")
}
func (this *readwrite)out() {
fmt.Println("out()")
}
func (this *readwrite)scan() {
fmt.Println("scan()")
}
func (this *readwrite)input() {
fmt.Println("input()")
}
func Test() {
var _readwrite readwrite
_readwrite.echo()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
多种继承接口,需要实现接口的所有方法
interface { interfaceA interfaceB }
1
2
3
4一个变量实现了接口当中所有方法,接口就可以指向这个变量
# 面向对象设计思想
- 同一个模块内的各个元素之间要高度紧密
- 不同模块之间的新湖依存度不比这个紧密
- 高内聚低耦合
# 面向对象的作用
- 易维护
- 质量高
- 效率高
- 易扩展