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 () }