前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go语言之Interface(二)

Go语言之Interface(二)

作者头像
程序员同行者
发布2019-02-22 09:43:58
3090
发布2019-02-22 09:43:58
举报
使用指针接收器和值接收器实现接口
type Describer interface {
    Describe()
}

type Person struct {
    name string
    age  int
}

func (p Person) Describe() {
    fmt.Printf("%s is %d years old\n", p.name, p.age)
}

type Address struct {
    state   string
    country string
}

func (a *Address) Describe() {
    fmt.Printf("State %s Country %s", a.state, a.country)
}

    var d1 Describer
    p1 := Person{"Sam", 25}
    d1 = p1
    d1.Describe()
    p2 := Person{"James", 39}
    d1 = &p2
    d1.Describe()

    var d2 Describer
    a := Address{"Beijing", "China"}
    d2 = &a
    d2.Describe()
实现多个接口
type NormalSalary interface {
    DisplaySalary()
}

type LevaeSalary interface {
    CalculateLeaveLeft() int
}

type Employee struct {
    firstName   string
    lastName    string
    basicPay    int
    pf          int
    totalLeaves int
    leavesTaken int
}

func (e Employee) DisplaySalary() {
    fmt.Printf("%s %s has salary $%d\n", e.firstName, e.lastName, (e.basicPay + e.pf))
}

func (e Employee) CalculateLeaveLeft() int {
    return e.totalLeaves - e.leavesTaken
}

    e := Employee{
        firstName:   "Kevin",
        lastName:    "Lee",
        basicPay:    5000,
        pf:          200,
        totalLeaves: 30,
        leavesTaken: 5,
    }

    var ns NormalSalary = e
    ns.DisplaySalary()

    var l LevaeSalary = e
    fmt.Println("Leaves left =", l.CalculateLeaveLeft())
接口嵌套

Go语言中没有继承的概念,但是通过组合可以实现继承的效果

type NormalSalary interface {
    DisplaySalary()
}

type LevaeSalary interface {
    CalculateLeaveLeft() int
}

type SalaryOperator interface {
    NormalSalary
    LevaeSalary
}

    var empOp SalaryOperator = e
    empOp.DisplaySalary()
    fmt.Println("Leaves left = ", empOp.CalculateLeaveLeft())
接口零值

零值接口是nil,nil接口中的typevalue都是nil

type Describer interface {
    Describe()
}

    var d4 Describer
    if d4 == nil {
        fmt.Printf("d4 is nil and has type %T value %v\n", d4, d4)
    }

输出结果是

d4 is nil and has type <nil> value <nil>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用指针接收器和值接收器实现接口
  • 实现多个接口
  • 接口嵌套
  • 接口零值
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档