Golang语言中的interface是什么(上)

栏目: Go · 发布时间: 6年前

内容简介:interface是一组method签名的组合,interface可以被任意对象实现,一个对象也可以实现多个interface。任意类型都实现了空interface(也就是包含0个method的interface),空interface可以存储任意类型的值。interface定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。

interface是一组method签名的组合,interface可以被任意对象实现,一个对象也可以实现多个interface。任意类型都实现了空interface(也就是包含0个method的interface),空interface可以存储任意类型的值。interface定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。

package main

import (
    "fmt"
)

// 定义struct
type Human struct {
    name  string
    age   int
    phone string
}
type Student struct {
    Human  // 匿名字段
    school string
    loan   float32
}
type Employee struct {
    Human   // 匿名字段
    company string
    money   float32
}

// Human对象实现SayHi()方法
func (h Human) SayHi() {
    fmt.Printf("Hi, I am %s, you can call me on %s\n", h.name, h.phone)
}

// Human对象实现Sing()方法
func (h Human) Sing(lyrics string) {
    fmt.Println("La la la...", lyrics)
}

// Human对象实现Guzzle()方法
func (h Human) Guzzle(beerStein string) {
    fmt.Println("Guzzle Guzzle Guzzle...", beerStein)
}

// Employee对象重写SayHi()方法
func (e Employee) SayHi() {
    fmt.Printf("Hi I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone)
}

// Student对象实现BorrowMoney()方法
func (s Student) BorrowMoney(amount float32) {
    s.loan += amount
}

// Employee对象实现SpendSalary()方法
func (e Employee) SpendSalary(amount float32) {
    e.money -= amount
}

// 定义interface,interface是一组method签名的组合
// interface可以被任意对象实现,一个对象也可以实现多个interface
// 任意类型都实现了空interface(也就是包含0个method的interface)
// 空interface可以存储任意类型的值
// interface Men的3个method被Human,Student,Employee实现,也就是这3个对象都实现了interface Men。即:
// interface定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。
type Men interface {
    SayHi()
    Sing(lyrice string)
    Guzzle(beerStein string)
}

// interface YoungChap的BorrowMoney() method只被Student对象实现,也就是只有Student实现了YoungChap
type YoungChap interface {
    SayHi()
    Sing(song string)
    BorrowMoney(amount float32)
}

// interface ElderlyGent的SpendSalary() method只被Employee对象实现,也就是只有Employee实现了ElderlyGent
type ElderlyGent interface {
    SayHi()
    Sing(song string)
    SpendSalary(amount float32)
}

func main() {
    // 定义Student类型的变量
    lucy := Student{Human{"lucy", 19, "10086"}, "tsinghua", 100.00}
    lily := Student{Human{"lily", 19, "10086"}, "tsinghua", 100.00}
    liming := Student{Human{"liming", 19, "10086"}, "tsinghua", 100.00}
    // 定义Employee类型的变量
    tom := Employee{Human{"tom", 29, "10000"}, "Google", 200.00}
    // 定义Men类型的变量i
    var i Men
    // i存储Student
    i = lucy
    fmt.Println("This is lucy, a student:")
    i.SayHi()
    i.Sing("Happy Birthday")
    i.Guzzle("Ha ha ha...")

    // i存储Employee
    i = tom
    fmt.Println("This is tom, an Employee:")
    i.SayHi()

    // 定义slice Men,包含Men类型元素的切片,这个slice可以被赋予实现了Men接口的任意结构的对象
    fmt.Println("Let's use a slice of Men and see what happens:")
    x := make([]Men, 3)
    // 三个不同类型(不同Method)的元素,实现了同一个interface(Men)
    x[0], x[1], x[2] = lucy, lily, liming
    for _, value := range x {
        value.SayHi()
    }
}

以上所述就是小编给大家介绍的《Golang语言中的interface是什么(上)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

SNS网站构建

SNS网站构建

Gavin Bell / 张卫星、李占波、徐静 / 机械工业出版社 / 2011-2 / 69.00元

过去的十年里,Web成为了非常重要的社交工具。社会活动已经超出了BBS这个概念,而指范围更广的互联网。大多数人对Facebook、MySpace以及Twitter并不陌生,事实上,现在很多人在网络上都有个人档案。社会媒体已经成为我们生活的一部分,它可以让我们的生活更加美 好,也可以使其更糟糕,像公民新闻这样的表达已变得很常见。仅仅Facebook就有两亿注册用户。那么在这个新领域中到底有什么奥秘呢......一起来看看 《SNS网站构建》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具