我感觉个还好理解,
至少比当初入JAVA时更容易理解,
可能是以前的学习为现在打下了基础吧。
又或许是最近三年的程序编写,长了见识吧~
package main
import (
"fmt"
)
type notifier interface {
notify()
}
type user struct {
name string
email string
}
type admin struct {
user
level string
}
func (u user) notify() {
fmt.Printf("Sending User Email To %s<%s>\n",
u.name,
u.email)
}
func (u admin) notify() {
fmt.Printf("Sending Admin Email To %s<%s>\n",
u.name,
u.email)
}
func (u *user) changeEmail(email string) {
u.email = email
}
func main() {
bill := user{"Bill", "bill@email.com"}
sendNotification(bill)
lisa := admin{
user: user{
name: "Lisa",
email: "lisa@email.com",
},
level: "super",
}
sendNotification(lisa)
}
func sendNotification(n notifier) {
n.notify()
}