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


本文摘自网络,作者,侵删。

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

201808

相关说明


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

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

实现 class 类


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

  1. 通过type struct 定义一个struct【类】

    type rsaSecurity struct {
    }
    复制代码
  2. 再定义一个这个类的变量,也就是对象

    var RsaSecuritySrv rsaSecurity
    复制代码

    类似于构造函数的定义,也可以通过new一个对象来使用,二选一。

  3. 实现一个这个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. 在任何需要调用这个成员方法的时候,通过对象来调用

    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的对象,并对成员变量赋值

本文来自:51CTO博客

感谢作者:mob604756e7db9c

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

相关阅读 >>

Golang中slice作为参数会怎么样

手撸Golang etcd raft协议之5

Go生成Go动态库或静态库的方法

[系列] Go - 统一定义 api 错误码

Golang稀疏数组

awesome Go!高玩Gopher都会参考的Go资源集合

Golang nil 小知识

Golang 1.9什么时候发布

解析Golang iris怎么使用

手撸Golang Go与微服务 es-cqrs模式之2

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...