This is a creation in Article, where the information may have evolved or changed.
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Friends who have had a C + + language learning experience know that object-oriented mainly includes three basic features: encapsulation, inheritance, and polymorphism. Encapsulation, that is, the running of data and functions are bound together, C + + is mainly through the this pointer to complete; Inheritance means that classes can inherit attributes and functions from each other, polymorphism, which is mainly to use a unified interface to deal with common logic, Each class only needs to implement its own callback function according to the interface.
As a master of the Go language, Nature does not act on object-oriented. Compared to C + +, Java, C # and other object-oriented languages, its object-oriented 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.
Encapsulation features,
Package Mainimport "FMT" type data struct {val int}func (p_data* data) set (num int) {p_data.val = Num}func (p_data* data) Sho W () {FMT. Println (P_data.val)}func main () {p_data: = &data{4}p_data.set (5) p_data.show ()}
Inheritance attributes,
Package Mainimport ' FMT ' type parent struct {val Int}type child struct {parentnum Int}func main () {var c childc = Child{par Ent{1}, 2}fmt. Println (C.num) fmt. Println (C.val)}
Polymorphic Properties,
Package Mainimport "FMT" type Act interface {write ()}type xiaoming struct {}type xiaofang struct {}func (xm *xiaoming) writ E () {fmt. Println ("Xiaoming write")}func (XF *xiaofang) write () {FMT. Println ("Xiaofang write")}func main () {var w act;xm: = xiaoming{}xf: = Xiaofang{}w = &xmw.write () w = &xfw.write ()}
On the object-oriented, the go language behaves more concisely and directly. On the one hand, this comes from a thorough understanding of object-oriented by the language designer, and on the other hand, I think it comes from the Google Designer's summary of past experience. Personally feel that these characteristics are very interesting, interested friends can try for themselves.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.