您的位置:首页 > 编程语言 > Go语言

go语言多态接口样例

2017-06-30 13:45 513 查看
感觉比java玄幻啊~~~

package main

import (
"fmt"
)

type notifier interface{
notify()
}

type user struct {
name  string
email string
}

func (u *user) notify() {
fmt.Printf("Sending user email to %s<%s>\n",
u.name,
u.email)
}

type admin struct {
name string
email string
}

func (a *admin) notify() {
fmt.Printf("Sending admin email to %s<%s>\n",
a.name,
a.email)
}

//main is the entry of the program
func main() {
bill := user{"Bill", "bill@email.com"}

sendNotification(&bill)

lisa := admin{"Bill", "lisa@email.com"}

sendNotification(&lisa)

}

func sendNotification(n notifier) {
n.notify()
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: