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