Go语言入门(一)——接口的实现

package main

import (
    "fmt"
)

/*接口*/
type IFly interface {
    Fly()
}

/*Bird结构体*/
type Bird struct {
}

/*接口方法的实现*/
func (b *Bird) Fly() {
    fmt.Println("Bird can fly")
}

/*Duck结构体*/
type Duck struct {
}

/*接口方法的实现*/
func (d *Duck) Fly() {
    fmt.Println("Duck can not fly")
}

func main() {
    b := new(Bird)
    b.Fly()
    d := new(Duck)
    d.Fly()
}

输出结果

你可能感兴趣的:(go语言)