The three main features of object-oriented in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Friends who have had a JAVA language learning experience know that object-oriented mainly includes three basic features: encapsulation, inheritance, and polymorphism. Encapsulation, that is, the data and functions are bound together, JAVA is mainly through the super pointer to do, inheritance, refers to the class can inherit attributes and functions, polymorphism, mainly with a unified interface to deal with common logic, each class You just have to implement your own callback function by the interface.

As a master of the Go language, Nature does not act on object-oriented. Compared to the object-oriented languages such as Java, C #, C + +, the object-oriented language is simpler and easier to understand. Below, we might as well use three simple examples to illustrate what object-oriented is in the go language.

Package characteristics

Package main  Import "FMT"  type data struct {      val int  }  func (P_data *data) set (num int) {      p_data. val = num  }  func (P_data *data) show () {      fmt. Println (p_data.val)  }  func main () {      p_data: = &data{4}      p_data.set (5)      p_data.show ()  }  

Inheritance attributes

Package main  import ' FMT '  type parent struct {      val int  }  type child struct {      parent      num int< c7/>}  func main () {      var c child      C = Child{parent{1}, 2}      FMT. Println (c.num)      FMT. Println (c.val)  }

Polymorphic Properties

Package main  Import "FMT"  Type Act interface {      write ()  }  type xiaoming struct {  }  Type Xiaofang struct {  }  func (XM *xiaoming) write () {      fmt. Println ("Xiaoming write")  }  func (XF *xiaofang) write () {      fmt. Println ("Xiaofang write")  }  func main () {      var w act;      XM: = xiaoming{}      XF: = xiaofang{}      w = &xm      w.write ()      w = &xf      w.write ()  }
Related Article

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.