代码在这里:http://play.golang.org/p/koMmlbmwYk代码:packagemainimport"fmt"typeHumanstruct{namestringageintphonestring}typeStudentstruct{Human//匿名字段schoolstring}typeEmployeestruct{Human//匿名字段companystring}//Human定义methodfunc(h*Human)SayHi(){fmt.Printf("Hi,Iam%syoucancallmeon%s\n",h.name,h.phone)}//Employee的method重写Human的methodfunc(e*Employee)SayHi(){fmt.Printf("Hi,Iam%s,Iworkat%s.Callmeon%s\n",e.name,e.company,e.phone)//Yesyoucansplitinto2lineshere.}funcmain(){mark:=Student{Human{"Mark",25,"222-222-YYYY"},"MIT"}sam:=Employee{Human{"Sam",45,"111-888-XXXX"},"GolangInc"}mark.SayHi()sam.SayHi()}对于这句://Human定义methodfunc(h*Human)SayHi(){fmt.Printf("Hi,Iam%syoucancallmeon%s\n",h.name,h.phone)}//Employee的method重写Human的methodfunc(e*Employee)SayHi(){fmt.Printf("Hi,Iam%s,Iworkat%s.Callmeon%s\n",e.name,e.company,e.phone)//Yesyoucansplitinto2lineshere.}receiver是指向Houman的指针,但如果我改成这样://Human定义methodfunc(hHuman)SayHi(){fmt.Printf("Hi,Iam%syoucancallmeon%s\n",h.name,h.phone)}//Employee的method重写Human的methodfunc(eEmployee)SayHi(){fmt.Printf("Hi,Iam%s,Iworkat%s.Callmeon%s\n",e.name,e.company,e.phone)//Yesyoucansplitinto2lineshere.}针向本身,结果输出效果是一样的。这是怎么回事呢?
慕斯王
相关分类