1. Anonymous combination
The go language also provides inheritance, but it takes a combination of methods, so we call it an anonymous combination:
PackageMainImport "FMT"//define base classtypeBasestruct{Namestring}2 Member methods related to//base classfunc(Base *base) A () {fmt. Println ("Base method A called ...")}func(Base *base) B () {fmt. Println ("Base method B called ...")}//define sub-classtypeSonstruct{Base//"Inherit" base class}//Override the base class's B methodfunc(son *son) B () {son. Base.b ()//Call the base class's B methodFmt. Println ("Son method B called ...")}funcMain () {son: = son{base{"Mchenys"}} son. B ()//Call the subclass's override to the base class of the B methodSon. A ()//Call subclass a method that inherits to the base class}
Output Result:
method B called...Son method B called...Base method A called...
The code above defines a base class (implements A () and B () two member methods), and then defines a
Son, the class inherits from the base class and overwrites the B () method, which calls the B () method of the base class first.
When a derived class son does not overwrite a member method of base class base, the corresponding method is "inherited", for example, in the
In the example above, call son. A () and call son. The Base.a () effect is the same.
Unlike other languages, the go language clearly tells you what a class's memory layout is. In addition, in the Go language you Also
You can modify the memory layout as you like:
typestruct { // 其他成员 Base}
This code is semantically not different from the example given above, but the memory layout has changed. "Base class" base
At the end of the "derived class" son.
In addition, in the Go language, you can also "derive" from a type as a pointer:
Type Son struct {
*base
...//other Members
}
The go code still has a "derivation" effect, but when son creates an instance, it needs to provide a base class externally
The pointer to the instance.
A log is combined anonymously, as shown below. Logger pointers:
typestruct { string *log.Logger}
After the appropriate assignment, we can comfortably borrow all the Log.logger in all member methods of the job type
The method of supply. For example, the following wording:
Func (Job *job) Start () {
Job. Log ("Starting now ...")//Note: The Receiver of the log function is still the Log.logger pointer
...//do something
Job. Log ("started.")
}
For the job's creator, he doesn't even have to be aware of log. The existence of the logger type, which is the anonymous combination of
Charm. In practical work, only reasonable use can maximize the value of this function.
2. Conflict of Name
We have to look at the name collisions in the interface combinations, such as the following combinations:
package mainimport( "fmt")typestruct { string}typestruct { X string//相同名字的属性名会覆盖父类的属性}func main(){ y := Y{X{"XChenys"},"YChenys"} fmt.Println("y.Name = "//y.Name = YChenys}
Is there a problem with combining types and grouped types that contain a name member? The answer is in the negative. All
Access to the name member of type Y is only accessible to the outermost name variable, and the X.name variable is equivalent to being overwritten.
Go language Learning (13) object-oriented programming-inheritance