Golang面向对象编程之继承&虚基类【组合&接口】

简介: Golang面向对象编程之继承&虚基类【组合&接口】

Golang面向对象编程之继承&虚基类【组合&接口】


相关说明

Golang里面没有像C++一样有继承相关的概念,但是我们却可以实现继承相关的用法,这就要用到struct、interface这两个结构。

Golang里面有组合的概念,也就是一个struct 里面可以包含一个或者多个struct,struct可以近似理解为面向对象编程中的class,但是不能等同,有很多区别。如果一个struct实现了某个接口的所有方法,那么只要是包含这个struct的所有其他struct也都是实现了这个接口的所有方法

实现 class 类

要想实现class类的用法,那么就要用到struct结构,通过给定struct定义某个成员变量或成员方法就可以实现类的方法

  1. 通过type struct 定义一个struct【类】
type rsaSecurity struct {
}
  1. 再定义一个这个类的变量,也就是对象
var RsaSecuritySrv rsaSecurity
  1. 类似于构造函数的定义,也可以通过new一个对象来使用,二选一。
  2. 实现一个这个struct类的方法,需要注意要显示的声明所属对象,即(rs *rsaSecurity)
// 加密
func (rs *rsaSecurity) RsaEncrypt(origData []byte) ([]byte, error) {
    //解密pem格式的公钥
    block, _ := pem.Decode(publicKey)
    if block == nil {
        return nil, errors.New("public key error")
    }
    // 解析公钥
    pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err != nil {
        return nil, err
    }
    // 类型断言
    pub := pubInterface.(*rsa.PublicKey)
    //加密
    return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
  1. 注意这里是否使用指针,在于是否能够是否需要彻底修改成员变量的值。
  2. 在任何需要调用这个成员方法的时候,通过对象来调用
func main() {
    data, _ := RsaSecuritySrv.RsaEncrypt([]byte(encrypt))
}

实现继承

直接上代码如下,很简单,主要就是一个struct里面包含一个匿名的struct,也就是通过匿名组合来实现

package main
import (
    "fmt"
)
// 【基类】
//定义一个最基础的struct类MsgModel,里面包含一个成员变量msgId
type MsgModel struct {
    msgId   int
    msgType int
}
// MsgModel的一个成员方法,用来设置msgId
func (msg *MsgModel) SetId(msgId int) {
    msg.msgId = msgId
}
func (msg *MsgModel) SetType(msgType int) {
    msg.msgType = msgType
}
//【子类】
// 再定义一个struct为GroupMsgModel,包含了MsgModel,即组合,但是并没有给定MsgModel任何名字,因此是匿名组合
type GroupMsgModel struct {
    MsgModel
    // 如果子类也包含一个基类的一样的成员变量,那么通过子类设置和获取得到的变量都是基类的
    msgId int
}
func (group *GroupMsgModel) GetId() int {
    return group.msgId
}
/*
func (group *GroupMsgModel) SetId(msgId int) {
    group.msgId = msgId
}
*/
func main() {
    group := &GroupMsgModel{}
    group.SetId(123)
    group.SetType(1)
    fmt.Println("group.msgId =", group.msgId, "\tgroup.MsgModel.msgId =", group.MsgModel.msgId)
    fmt.Println("group.msgType =", group.msgType, "\tgroup.MsgModel.msgType =", group.MsgModel.msgType)
}

实现虚基类的用法

Golang可以通过匿名组合来实现继承。

Golang可以interface + struct来实现虚基类的用法,必须要实现interface中定义的方法。

1,定义一个interface接口MsgModel,包含了一些方法。

type MsgModel interface {
    Persist(context context.Context, msg interface{}) bool
    PersistOnSensitive(context context.Context, session_type, level, SensitiveStatus int32, msg interface{}) bool
}

3,定义一个类型msgModelImpl,用来实现接口类型

定义一个struct用来实现接口类型
type msgModelImpl struct{}
定义一个变量MsgModelImpl等于msgModelImpl,相当于可以通过MsgModelImpl来调用msgModelImpl的成员
var MsgModelImpl = msgModelImpl{}
实现接口的两个方法
func (m msgModelImpl) Persist(context context.Context, msgIface interface{}) bool {
// 具体实现省略
}
func (m msgModelImpl) UpdateDbContent(context context.Context, msgIface interface{}) bool {
// 具体实现省略
}

4, 定义一个struct类型的msgService,包含上述接口类型MsgModel,相当于组合了。这样的话,这个类型就需要要实现接口方法。

type msgService struct {
   msgModel MsgModel
}

5, 再定义一个变量MsgService,首字母大写,并且赋值为msgService对象,同时给成员msgModel赋值为上述已经实现了接口的struct对象MsgModelImpl。

将上述已经实现接口类型的类型(MsgModelImpl) 赋值给此变量(此变量并且要是包含了接口类型的类型), 然后这个变量就可以供外部调用
var  MsgService = msgService{
        msgModel:  MsgModelImpl,
}

6, 通过MsgService调用接口方法

7, 小结:

  • MsgModel 是一个interface
  • interface 是一组抽象方法(未具体实现的方法/仅包含方法名参数返回值的方法)的集合
  • msgModelImpl是一个struct,它实现了MsgModel这个interface的所有方法
  • 如果实现了 interface 中的所有方法,即该类/对象就实现了该接口
  • MsgModelImpl是msgModelImpl这个struct的对象
  • msgService是一个struct,它包含了MsgModel,相当于组合
  • MsgService是msgService这个struct的对象,并对成员变量赋值

收录于合集 #Go 语言

14

上一篇Golang面向对象编程之构造函数【struct&new】下一篇Golang的反射reflect深入理解和示例


相关文章
|
2月前
|
安全 Java 编译器
第十一章 Golang面向对象编程(下)
第十一章 Golang面向对象编程(下)
43 2
|
1天前
|
JSON Go 数据格式
golang学习7,glang的web的restful接口结构体传参
golang学习7,glang的web的restful接口结构体传参
|
1天前
|
JSON Go 数据格式
golang学习6,glang的web的restful接口传参
golang学习6,glang的web的restful接口传参
|
1天前
|
JSON Go 数据格式
golang学习5,glang的web的restful接口
golang学习5,glang的web的restful接口
|
1天前
|
Go
golang学习4,glang的web接口
golang学习4,glang的web接口
|
16天前
|
安全 Go
Golang深入浅出之-接口(Interfaces)详解:抽象、实现与空接口
【4月更文挑战第22天】Go语言接口提供抽象能力,允许类型在不暴露实现细节的情况下遵循行为约定。接口定义了一组方法签名,类型实现这些方法即实现接口,无需显式声明。接口实现是隐式的,通过确保类型具有接口所需方法来实现。空接口`interface{}`接受所有类型,常用于处理任意类型值。然而,滥用空接口可能丧失类型安全性。理解接口、隐式实现和空接口的使用能帮助编写更健壮的代码。正确使用避免方法,如确保方法签名匹配、检查接口实现和谨慎处理空接口,是关键。
21 1
|
16天前
|
Go 开发者
Golang深入浅出之-Go语言方法与接收者:面向对象编程初探
【4月更文挑战第22天】Go语言无类和继承,但通过方法与接收者实现OOP。方法是带有接收者的特殊函数,接收者决定方法可作用于哪些类型。值接收者不会改变原始值,指针接收者则会。每个类型有相关方法集,满足接口所有方法即实现该接口。理解并正确使用这些概念能避免常见问题,写出高效代码。Go的OOP机制虽不同于传统,但具有灵活性和实用性。
23 1
|
2月前
|
程序员 Go
第十章 Golang面向对象编程(上)
第十章 Golang面向对象编程(上)
40 2
|
4月前
|
XML Go 数据格式
【微信公众号开发】基于golang的公众号开发——接入消息自动回复接口
【微信公众号开发】基于golang的公众号开发——接入消息自动回复接口
152 0
|
4月前
|
Go
浅谈Golang接口interface
浅谈Golang接口interface
19 0