an go interface example

an go interface example

 

参考(https://zhuanlan.zhihu.com/p/111496672)

 

package main

import (

    "fmt"

)

type NoticeInterface interface {  

   seedEmail()  

   seedSMS()  

}  

 

type Student struct {  

   Name string  

  Email string  

  Phone string  

}  

 

 

 

func (Student *Student)seedEmail()  {  

   fmt.Printf("seedEmail to %s\r\n",Student.Email)  

}  

 

func (Student *Student)seedSMS()  {  

   fmt.Printf("seedSMS to %s\r\n",Student.Email)  

}

 

 

type Teacher struct {  

   Name string  

  Email string  

  Phone string 

  Salary string 

}  

 

func (Teacher  *Teacher )seedEmail()  {  

   fmt.Printf("Te seedEmail to %s\r\n",Teacher.Email)  

}  

 

func (Teacher  *Teacher )seedSMS()  {  

   fmt.Printf("Te seedSMS to %s\r\n",Teacher.Email)  

}

 

 

func seedNotice(notice NoticeInterface)  {  

   notice.seedEmail()  

   notice.seedSMS()  

}

 

func main() {

    student := 

    Student{"andy","jiumengfadian@live.com","10086"}  

   //seedNotice(student)  //这里会产生一个错误

   seedNotice(&student)

 

    teacher := 

   Teacher{"John","john.z@live.com","10086","30000"}

   seedNotice(&teacher)

}

 

(log)

seedEmail to jiumengfadian@live.com

seedSMS to jiumengfadian@live.com

Te seedEmail to john.z@live.com

Te seedSMS to john.z@live.com

 

posted @ 2021-04-21 10:03  iadacm  阅读(54)  评论(0编辑  收藏  举报