JAVA language learning experience had friends all know, the object-oriented mainly includes three basic characteristics: encapsulation, inheritance and polymorphism. Encapsulation, refers to the function and operation of data bundled together, JAVA is mainly done by super pointer; inheritance, refers to inherit the properties and function between each class; polymorphism, mainly with a unified interface to handle General logic, each class only in accordance with its own interface callback function on it.
Go as a master of language, object-oriented nature does nothing in it. Compared java, C #, C ++ and other object-oriented languages, for its object-oriented simpler and easier to understand. Below, we may wish to use three simple examples to illustrate object-oriented language go under what.
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 feature
package main import "fmt" type parent struct { val int } type child struct { parent num int } 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() }