The go language uses a combination of methods to implement multiple inheritance _golang

Source: Internet
Author: User
Tags inheritance

This example describes how the go language implements multiple inheritance using a combination of methods. Share to everyone for your reference. The implementation methods are as follows:

Multiple inheritance is not supported in most object-oriented programming languages. Because in the class based system, multiple inheritance greatly increases the complexity of the compiler.
The go language implements inheritance using a combination of methods, so it can also be simple to implement multiple inheritance.

Copy Code code as follows:
Implementing multiple inheritance using a combination of methods
Type Phone struct{}

Func (P *phone) call () string {
Return to "Ring Ring"
}

Type Camera struct{}

Func (c *camera) takeapicture () string {
Return "click"
}

Multiple inheritance
Type cameraphone struct {
Camera
Phone
}

Func structTest0803 () {
CP: = new (Cameraphone)
Fmt. Println ("Our new cameraphone exhibits multiple behaviors ...")
Fmt. PRINTLN ("It exhibits behavior of a Camera:", CP. Takeapicture ())
Fmt. Println ("It works like a Phone too:", CP. Call ())

/*output:
Our new cameraphone exhibits multiple behaviors ...
It exhibits behavior of a Camera:click
It works like a Phone too:ring ring
*/
}

I hope this article will help you with your go language program.

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.