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

6,084 阅读4分钟

[TOC]

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)
    }
    

    注意这里是否使用指针,在于是否能够是否需要彻底修改成员变量的值。

  4. 在任何需要调用这个成员方法的时候,通过对象来调用

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

【"欢迎关注我的微信公众号:Linux 服务端系统研发,后面会大力通过微信公众号发送优质文章"】

我的微信公众号