6.9. 第7节:多态接口编程

未匹配的标注
本文档最新版为 2023,旧版本可能放弃维护,推荐阅读最新版!
package main

import "fmt"

//定义接口
type inter interface {
    //通信接口
    CSocketProtocol()
    //加密接口
    CEncDesProtocol()
}

//厂商1类
type CSckImp1 struct {
    data   string
    socket string
}

//厂商2类
type CSckImp2 struct {
    data   string
    socket string
    value int
}

func (cs1 *CSckImp1) CSocketProtocol() {
    fmt.Printf("厂商1的通信接口数据为:%s\n", cs1.socket)
}
func (cs1 *CSckImp1) CEncDesProtocol() {
    fmt.Printf("厂商1的加密接口数据为:%s\n", cs1.data)
}

func (cs2 *CSckImp2) CSocketProtocol() {
    fmt.Printf("厂商2的通信接口数据为:%s\n", cs2.socket)
}
func (cs2 *CSckImp2) CEncDesProtocol() {
    fmt.Printf("厂商2的加密接口数据为:%s 数值为:%d\n", cs2.data,cs2.value)
}

//多态实现
func framework(i inter) {
    i.CSocketProtocol()
    i.CEncDesProtocol()
}

func main() {
    cs1 := CSckImp1{"厂商1的加密数据", "厂商1的通信数据"}
    framework(&cs1)

    cs2 := CSckImp2{"厂商2的加密数据", "厂商2的通信数据",123}
    framework(&cs2)
}

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~