How Go is inherited with a combination

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

Go offers great concurrency support, but go doesn't support full object orientation. This does not mean that go does not support object-oriented, and the OO system of Go is very lightweight and the learning cost is minimized. Although the go loses some OO convenience features in order to make this lightweight object-oriented, the higher efficiency and multiple return values compensate for this.


Those languages that fully support object-oriented language generally have inherited functionality. Inheritance has a great advantage, the simplest to say, is that you can write less code. Of course, inheritance is not just a matter of giving you a few keystrokes, it can also be a better abstraction of the relationship between the various types of programs.


The OO system of Go does not support inheritance, but you can use a method called " combination" in go to implement inheritance. Two programs are shown below, and you will see the combination of go.


Inheritance in C + +:

#include <iostream>using namespace Std;class person {public:    void Say ();}; void Person::say () {    cout << "I ' m a person." << Endl;} Inherit class Student:public person {};int main () {    Student s;    S.say ();    return 0;}

The equivalent Go program:

Package MainType person struct {}func (P *person) Say () {    println ("I ' m a person.")} Combination type Student struct {    person}func main () {    var s Student    S.say ()}

after the two programs run, the results are:

I ' m a person.


You can see that go can "inherit" all of its behavior by combining another type, which is intuitive. However, the meaning of the two sections of the code for C + + and go is actually slightly different. The class inheritance of C + + indicates that person is a parent class of student and has a hierarchical relationship. But the go combination expresses a student is an individual, so student contains all the behavior of person, that is, what people can do student can do, student is also an individual.


Whether it's inheritance or composition, the "copy" behavior can be overridden ~

I hope this blog post lets you learn the combination of go.



If reproduced please specify the source: Http://blog.csdn.net/gophers


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.