Go Learning notes: Anonymous members

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

began to understand what is anonymous members, read several articles, finally understand, at least self-think is.

Why do we need anonymous members?

Have to start with OO. OO is a necessary part of modern computer programming languages. However, different languages are not the same for how to implement various concepts in OO. Go provides support for OO, but it is a neat and straightforward approach that does not resemble the extends, implements keywords in Java, but rather uses anonymous members directly and is implemented through "delegation." For example, the following code:


Type E interface{

Start ()

Stop ()

Renewal (x int)

}

Type W Interface {

Open ()

Close ()

Renewal ()

}

Type Car struct {
E
W
}

With this, when you have created an instance of car, you can use:


Car. Start ()
Car. Stop ()
Car. Open ()
Car. Close ()

There is no difference between using car and your own method. Is there extends and implements mean!

It is important to note that this is not possible due to the presence of the same name Method (renewal):

Car. Renewal ()//Compilation error

and must be:

Car. E.renewal (5)

Or

Car. W.renewal ()


It seems that with this anonymous member and delegation, several concepts in OO are included.



Related Article

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.