golang接口实现

2022-08-08  本文已影响0人  耍帅oldboy
package main

import (
    "fmt"
)

type Animals interface {
    Dog() string
    Cat() string
}

type My struct {
}

func (this My) Dog() string {
    return "Dog"
}

func (this My) Cat() string {
    return "Cat"
}

func printDog(animals Animals) {
    fmt.Println(animals.Dog())
}

func printCat(animals Animals) {
    fmt.Println(animals.Cat())
}

func main() {
    var my My
       //My结构体实现 Animals
    printDog(my)
    printCat(my)
}

执行结果

Dog
Cat
上一篇 下一篇

猜你喜欢

热点阅读