Go language implements a Polymorphic function instance in C + + _golang

Source: Internet
Author: User

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.

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.