Golang object-oriented programming
Author: Yin Zhengjie
Copyright: original works, declined to reprint! Otherwise held liable.
Go reason why object-oriented language with C ++, Java and Python languages of these different (on a lesser extent), because it does not support inheritance, only supports aggregation (also called combination) and embedding. Next we work together to learn about object-oriented programming language Go right.
A. Object-oriented programming ideas
Time just popular object-oriented programming, inheritance is one of the biggest advantages it was first touted. But after load after dozens of practice, the fact that this feature is also some significant drawbacks, particularly when used in the maintenance of large systems. Go language is proposed interface-oriented programming. Common programming mode: process-oriented (function-oriented programming): typical: C language advantages: the process clear, easy to read the code. Disadvantages: the degree of coupling is too high, is not conducive to project iteration. Object-oriented programming: typical: C ++ , the Java, Python, golang and so on. Advantages: decoupling. Disadvantages: Code abstraction is too high and difficult to read. Object-oriented three elements: packaging assembly: the operation data and are assembled together. Hidden data: Foreign exposed only some of the interface, the interface accessed through the object. For example, the driver uses the car, do not understand the details of the construction of the car, just need to know what parts how to drive on the line, stepped on the accelerator can run, you can not understand the principles of maneuver them. Inheritance multi multiplexing, inherited do not write their own. Multiple inheritance little modification, OCP (Open -closed Principle), use inheritance to change to reflect the personality. Polymorphic object-oriented programming where most flexible, dynamic binding. Polymerization with most other object-oriented languages and inheritance is different, Go language support only the polymerization (also called combination) and embedding.
II. Initialization and definition of the structure
main Package Import ( " FMT " ) type Person struct { the Name String Age int Gender String } type Student struct { Person // embedded Person attributes through an anonymous combination. Float64 Score } type Teacher struct { Person // through anonymous combined attributes embedded Person. Course, String } type Schoolmaster struct { Person // embedded Person attributes through an anonymous combination. CarBrandString } FUNC main () { / * * The first initialized: the first assignment definitions * / S1: = Student {} s1.Name = " Jason Yin " fmt.Println (S1) fmt.Printf ( " % V + \ n-\ n- ' , S1) // "+ V representing each print field structure" / * * the second initialize: direct initialization * / S2: = {the Person Teacher { " Yinzheng Jie " , 18 is , " Boy " }, " Go concurrent programming " } fmt.Println (S2) fmt.Printf ( " % V + \ n-\ n- " , S2) / * * Third assignment mode: initialization part assignment field * / S3: = {CarBrand Schoolmaster: " Toyota " , the Person: the Person {the Name: " JasonYin king strongest " }} fmt.Println (S3) fmt.Printf ( " % V + \ n- " , S3) }
III. Oriented Programming Interface