Golang面向對象編程之繼承&虛基類【組合&接口】

Golang面向對象編程之繼承&虛基類【組合&接口】

201808編程

相關說明


Golang裏面沒有像C++同樣有繼承相關的概念,可是咱們卻能夠實現繼承相關的用法,這就要用到struct、interface這兩個結構。markdown

Golang裏面有組合的概念,也就是一個struct 裏面能夠包含一個或者多個struct,struct能夠近似理解爲面向對象編程中的class,可是不能等同,有不少區別。若是一個struct實現了某個接口的全部方法,那麼只要是包含這個struct的全部其餘struct也都是實現了這個接口的全部方法session

實現 class 類


要想實現class類的用法,那麼就要用到struct結構,經過給定struct定義某個成員變量或成員方法就能夠實現類的方法ide

  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. 在任何須要調用這個成員方法的時候,經過對象來調用code

    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的對象,並對成員變量賦值
相關文章
相關標籤/搜索