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.