package main

import "fmt"

type Animal interface {
    run()
    walk()
}

type Dog struct {
    Id int
}

func (dog Dog) run()  {
    fmt.Printf("I am Dog,I can Run!\n")
}

func (dog Dog) walk(){
    fmt.Printf("I am Dog,I can walk!\n")
}

type Pig struct {
    Id int
}

func (pig Pig) run()  {
    fmt.Printf("I am Pig,I can Run!\n")
}

func main() {
    dog := Dog{100}
    var animal0 interface{} = dog
    if _, ok := animal0.(Animal); ok {
        fmt.Printf("animal0 implement Animal interface!\n")
    }else {
        fmt.Printf("animal0 not implement Animal interface!\n")
    }

    pig:=Pig{18}
    var animal1 interface{} = pig
    if _, ok := animal1.(Animal); ok {
        fmt.Printf("animal1 implement Animal interface!\n")
    }else {
        fmt.Printf("animal1 not implement Animal interface!\n")
    }

}

  

相关文章:

  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
  • 2022-01-30
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
相关资源
相似解决方案