Go 语言Interface

go interface

类型1:eFace = {类型,地址}

类型2:iFace={{t *iTab},地址}

// ==============

type Animal interface {

    Speak() string

}

// ==============

type Dog struct {

}

func (d Dog) Speak() string {

    return "Woof!"

}

// ==============

type Cat struct {

}

func (c Cat) Speak() string {

    return "Meow!"

}

// ==============

type JavaProgrammer struct {

}

func (j JavaProgrammer) Speak() string {

    return "Design patterns!"

}

// +++++++++++++++++++++++++++++++++

func main() {

    animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}}

    for _, animal := range animals {

        fmt.Println(animal.Speak())

    }

}

上一篇:ABAP 搜索帮助回调函数


下一篇:ts 学习笔记(二)变量声明和接口