Go three characteristics of object-oriented

#### Go in the object-oriented characteristics of the three 
last week because there are some things that stopped the more; more to stop this time, took some time to do a small project (https://github.com/yioMe/ node_wx_alipay_personalPay)
original project written by the node.js, according to the document with Go rewritten (Gin + MySQL);
1. Without tedious node installation;
2. no DB migration;
3. payment success callback optimization;
4. performance ;
5. Fixed an issue uploading two-dimensional code can not be identified;
however, only a small part of the rewrite function, not a problem to deal with daily personal payment, if you need to know a friend, whisper with exchange of learning;
---
Closer to home, Go in object-oriented characteristics of the conventional OOP languages, we learn about eleven;
###### package
encapsulating operation for the field is to package together abstract field, data to be protected in the interior, the program the other packages can only be authorized to operate on the
field of operation;
benefits of encapsulation:
1. hide implementation details;
2. the data can be verified, to ensure data security and reasonable;
---
step package implemented:
1. the first lowercase structure, the field can not be turned Other packages can not be used, similar to the JAVA Private;
2. the package structure where the factory mode provide some function, first letter capitalized, similar to the constructor;
3. a capitalized Set methods for properties judgment and assignment
4. Provide a capitalized Get method for obtaining the value of the property;
5. no special emphasis encapsulated in Go, a friend so other programming languages, other do not need to learn grammar characteristics Go, each programming language has its own characteristics;
Model Package 

Import "FMT" 

type struct {Student 
   the Name String 
   Age // int other packages are not directly accessible 
   score float64 // other packages can not directly access 
} 

// factory method, corresponding to the constructor 
func NewStudent (name string) * student { 
   return {& Student 
      the name: name, 
   } 
} 
// order to access and change the properties of the structure, one pair of write GetXxx / setXxx method 
// this type just received the name may be any name valid identifier 
func (this * Student) GetAge () {int 
   return this.age 
} 
FUNC (the this Student *) setAge (int Age) { 
   // the data can be verified in the Set method in 
   IF Age <0 || Age> {100 
      fmt.Println ( "Age IS Wrong") 
      return 
   } 
   this.age = Age 
}
func (this *student) GetScore() float64{
   return this.score
}
func (this *student) SetScore(score float64) {
   if score < 0 || score > 100 {
      fmt.Println("score is wrong")
      return
   }
   this.score = score
}

  

 
package main

import (
   "fmt"
   "personalPayment/model"
)

func main(){
   p := model.NewStudent("jack")
   p.SetAge(20)
   p.SetScore(200)
   fmt.Println(*p)
   fmt.Println(p.Name,"age=",p.GetAge(),"score=",p.GetScore())
}

  


---
###### Inheritance
1. Inheritance can solve the problem of code reuse
2. When a plurality of structures and methods have the same attributes, these can be abstracted from what structural body of the base structure, in which the same properties and methods defined in the structure;
3. Go the inheritance is achieved by anonymous nested structure;
basic syntax:
type the Person struct {
the Name String
Age int
}

type struct {Student
the Person // nested structure , inheritance
Score float64
}
case:
Model Package 

Import "FMT" 
type Person {struct 
   the Name String 
   Age int 
} 
type struct {Student 
   Person 
   Score float64 // other packages can not directly access 
} 
type struct {Teacher 
   Person 
   class String 
} 

// student factory method, which corresponds constructor 
FUNC NewStudent (name String) * Student { 
   return & Student {Person: Person { 
      the name: name, 
   }, 
   } 
   } 
// teacher factory method 
FUNC NewTeacher (name String) * teacher { 
   return & teacher {Person: Person { 
      the name: name, 
   }} 
} 
// methods public infrastructure  
func (this * person) GetAge ( ) int {
   return this.age 
}
FUNC (the this Person *) setAge (int Age) { 
   // the data can be verified in the Set method in 
   IF Age <0 || Age> {100 
      fmt.Println ( "Age IS Wrong") 
      return 
   } 
   this.age = Age 
} 
// methods students 
FUNC (the this student *) getScore () {float64 
   return this.score 
} 
FUNC (the this student *) SetScore (Score float64) { 
   IF Score <0 || Score> {100 
      fmt.Println ( " Wrong iS Score ") 
      return 
   } 
   this.score Score = 
} 
// methods teacher 
FUNC (* the this teacher) GetClass () {String 
   return this.class 
} 
func (this *teacher) SetClass(class string){
   this.class = class 
}

  

 
main Package 

Import ( 
   "FMT" 
   "personalPayment / Model" 
) 

FUNC main () { 
   s: = model.NewStudent ( "Jack") 
   // call method common structure 
   s.SetAge (20 is) 
   // calls their method 
   s .SetScore (100) 
   fmt.Println (s.Name, s.GetAge (), s.GetScore ()) 
   T: = model.NewTeacher ( "Tom") 
   // call method common structure 
   t.SetAge (40) 
   // call their own methods 
   t.SetClass ( "English") 
   fmt.Println (t.Name, t.GetAge (), t.GetClass ()) 
}

  


Use of inheritance and precautions
1. The structure can use all properties and methods of anonymous nested structure, regardless of uppercase and lowercase
2. A structural body and method for anonymous field structure can be simplified;
3. When the structure when anonymous structure and have the same attributes and methods, the compiler uses the principle of proximity, if necessary field access anonymous structural body
and a method name needs to be distinguished by an anonymous structure;
4. If a nested structure of a the name of the structure, this mode is called combination structures need to add the name or in the structural combination of access methods;
package main

import (
   "fmt"
)

type person struct {
   Name string
   Age int
   skill string
}
type student struct {
   person
   score float64
}
func (p *person) Say(){
   fmt.Println("I am a person")
}
func (p *person) Do(){
   fmt.Println("I am doing something")
}

type A struct {
   Name string
}
type B struct {
   A
   Name string
}
type C struct {
   Name string
}
type D struct {
   c C
   Age int
}
main FUNC () { 
   S: = {} & Student 
   s.person.Say () 
   s.person.Do () 
   s.person.Name = "Jack" 
   s.person.Age = 20 is 
   s.person.skill = "Speak" 
   fmt.Println (S *) 
   // anonymous attribute method can be simplified in structure as 
   s.Say () 
   s.Do () 
   s.Name = "jack2" 
   s.Age = 21 is 
   s.skill = "Laugh" 
   FMT .Println (S *) 
   // if the same property or method anonymous structure structural body, the compiler will use the principle of proximity, 
   // if the properties and methods need to access the anonymous structure required by name anonymous structure 
   a : B = { 
      A: A { 
         the Name: "AAA", 
      }, 
      the Name: "BBB", 
   } 
   fmt.Println (a.name) // BBB 
   fmt.Println (aAName) // AAA
   // For the combination, the structure need to add the name of the structure when accessing inherited methods or properties 
   D: D = { 
      C: {C the Name: "CCC"}, 
      Age: 20 is, 
   } 
   fmt.Println (d.Age ) // own property 
   fmt.Println (dcName) // access inherited attributes 
}

  The latest articles on personal micro-channel public number, welcome attention to the exchange of learning together

Guess you like

Origin www.cnblogs.com/Mail-maomao/p/11492644.html