Objective
As a new comer in the programming language, go language is a good development language, while paying attention to the efficiency of development while paying attention to the efficiency while absorbing the individuality. In the go, there is no concept of a class, but it can still be used struct+interface to implement the functionality of the class, and the following simple example demonstrates how to use go to simulate polymorphic behavior in C + +.
Sample code
Package main
import "OS"
import "FMT"
type Human interface {
SayHello ()
}
type Chinese struct {
name string
}
type 中文版 struct {
name string
}
func (c *chinese) SayHello () C18/>fmt. Println (c.name, "Say Hello, World")
}
func (e *english) SayHello () {
fmt. Println (e.name, "Says:hello,world")
}
func main () {
fmt. Println (len (OS). Args))
c: = chinese{"Doggy Man"}
e: = english{"Jorn"}
m: = map[int]human{}
m[0] = &c
m[1] = & E for
i:=0;i<2;i++ {
M[i].sayhello ()
}
}
Summarize
From the above example, it is very simple to implement polymorphism in C + + in Go, as long as the same interface is implemented.