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是什么(上)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

编程.建筑

编程.建筑

保罗·科茨 / 2012-9 / 45.00元

《编程•建筑》简单明了地介绍了计算机算法与程序用于建筑设计的历史,解释了基本的算法思想和计算机作为建筑设计工具的运用。作为计算机辅助设计的先驱,保罗·科茨通过多年讲授的计算、设计的教学内容和实例研究,向我们展示了算法思维。《编程•建筑》提供了详细、可操作的编码所需要的技术和哲学思想,给读者一些代码和算法例子的认识。一起来看看 《编程.建筑》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具