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

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

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

查看所有标签

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

数据挖掘导论

数据挖掘导论

(美)Pang-Ning Tan、Michael Steinbach、Vipin Kumar / 机械工业出版社 / 2010-9 / 59.00元

本书全面介绍了数据挖掘的理论和方法,着重介绍如何用数据挖掘知识解决各种实际问题,涉及学科领域众多,适用面广。 书中涵盖5个主题:数据、分类、关联分析、聚类和异常检测。除异常检测外,每个主题都包含两章:前面一章讲述基本概念、代表性算法和评估技术,后面一章较深入地讨论高级概念和算法。目的是使读者在透彻地理解数据挖掘基础的同时,还能了解更多重要的高级主题。 本书特色 ·包含大量的图表、......一起来看看 《数据挖掘导论》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具