Inheritance and rewriting of Go methods

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Inherited:

Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">mainimport ' fmt ' type Human struct {name string age int phone String}type Student struct {Human/ /anonymous field school string}type Employee struct {Human///anonymous field company string}//defined a Human above Methodfunc (H *human) sayhi () {FMT. Printf ("Hi, I am%s can call me on%s\n", H.name, H.phone)}func main () {mark: = student{human{"Mark", 25, "222-222 -yyyy "}," MIT "} sam: = employee{human{" Sam "," 111-888-xxxx "}," Golang Inc "} mark. Sayhi () Sam. Sayhi ()}</textarea>
12345678910111213141516171819202122232425262728293031 Package MainImport "FMT"type Human struct {    namestring     Ageint    Phonestring}type Student struct {    Human //anonymous field    Schoolstring}type Employee struct {    Human //anonymous field     Companystring}//Define a method on humanfunc (h *Human) Sayhi() {    FMT.Printf("Hi, I am%s You can call me on%s\n", h.name, h.Phone)}funcMain() {    Mark := Student{Human{"Mark", -, "222-222-yyyy"}, "MIT"}    Sam := Employee{Human{"Sam", $, "111-888-xxxx"}, "Golang Inc"}    Mark.Sayhi()    Sam.Sayhi()}

Rewrite:

<textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "DblClick" readonly style= "-MOZ-TAB-SIZE:4; -o-tab-size:4; -webkit-tab-size:4; Tab-size:4; FONT-SIZE:12PX!important; line-height:15px!important; " >package mainimport "FMT" type Human struct {name string age int phone String}type Student struct {Human//Anonymous Field School String}type Employee struct {Human//anonymous field company String}//human definition methodfunc (H *human) Sayhi () {f Mt. Printf ("Hi, I am%s can call me on%s\n", H.name, H.phone)}//employee method overrides Human Methodfunc (e *employee) Sayhi () { Fmt. Printf ("Hi, I am%s, I work in%s." Call me on%s\n ", E.name, E.company, E.phone)//yes can split into 2 lines H Ere.} Func Main () {mark: = student{human{"Mark", "222-222-yyyy"}, "MIT"} sam: = employee{human{"Sam", "a", "111-888-xx XX "}," Golang Inc "} mark. Sayhi () Sam. Sayhi ()} </textarea>
12345678910111213141516171819202122232425262728293031323334353637 Package MainImport "FMT"type Human struct {    namestring     Ageint    Phonestring}type Student struct {    Human //anonymous field    Schoolstring}type Employee struct {    Human //anonymous field     Companystring}//human Defining Methodfunc (h *Human) Sayhi() {    FMT.Printf("Hi, I am%s You can call me on%s\n", h.name, h.Phone)}//employee's method rewrites the method of humanfunc (E *Employee) Sayhi() {    FMT.Printf("Hi, I am%s, I work in%s." Call me on%s\n ", e.name,        e. Company, e.Phone) //yes can split into 2 lines here.}funcMain() {    Mark := Student{Human{"Mark", -, "222-222-yyyy"}, "MIT"}    Sam := Employee{Human{"Sam", $, "111-888-xxxx"}, "Golang Inc"}    Mark.Sayhi()    Sam.Sayhi()}

inheritance and rewriting of Go methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.