Use-a and Has-a in the Golang

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

Type person struct {    Name    string    address Address}type Address struct {number    string    Street string< C6/>city   string    state  string,    Zip    string}func (P *person) talk () {    fmt. Println ("Hi, my Name is", P.name)}func (P *address) location () {    //fmt. Println ("I ' m at", P.address.number, P.address.street, p.address.city, P.address.state, P.address.zip)    FMT. Println ("I ' m at", P.number, P.street, p.city, P.state, P.zip)}func main () {    p: = person{        Name: "Steve",        Ad dress:address{number            : "$",            Street: "Main", City            :   "Gotham", State            :  "NY",            Zip :    "01313",        },      }    p.talk ()    //P.location () wrong    p.address.location ()}

Above is a "use-a" code snippet, but note " address is still a different object, except that it exists in person. "

Type person struct {    Name string    address}type Address struct {number    string    Street string    City   string    State  string    Zip    string}func (P *person) talk () {    fmt. Println ("Hi, my Name is", P.name)}func (P *address) location () {    //fmt. Println ("I ' m at", P.address.number, P.address.street, p.address.city, P.address.state, P.address.zip)    FMT. Println ("I ' m at", P.number, P.street, p.city, P.state, P.zip)}func main () {    p: = person{        Name: "Steve",        Ad dress:address{number            : "$",            Street: "Main", City            :   "Gotham", State            :  "NY",            Zip :    "01313",        },      }       p.talk ()    p.location ()    p.address.location ()}
Above is a "is-a" code snippet, but note " is-a relationships are implemented by introducing anonymous domains (person). The person is an anonymous domain (anonymous field) for citizen, and the anonymous field gives only the object type, not the name of the type. With an anonymous domain, citizen can access all the attributes (domains) and methods in the person. ".

Part of the code and text to go is the object-oriented language? 》。

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.