The go language implements inheritance in the form of anonymous combinations

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
typeBasestruct{FirstName, LastNamestringAgefloat32}func(Base *base) Hasfeet () {fmt. Println (base. FirstName + base. LastName +"has feet! Base ")}func(Base *base) Flying () {fmt. Println ("Base Can flying!")}typeSubstruct{Base Areastring}func(Sub *sub) Flying () {Sub. Base.flying () fmt. Println ("Sub flying")}funcMain () {chk: =New(Sub) chk. Flying () Chk2: = &sub{base{"Bob","Steven",2.0},"China"} FMT. Println (CHK2. AREA)}

It is important to note that if a subclass overrides a member method of the base class, it needs to call the base class's member method of the same name in the member method of the subclass, and must be called in such an explicit way, rather than in a sub.Base.Flying() sub.Flying() way called an inherited method, so that an infinite loop occurs. The method that has been calling the subclass.

func  (sub *Sub) Flying () {Sub. Flying () //infinitely calls itself  FMT. Println ( "Sub flying" ) //will never execute here }  

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.