The Object Oriented Method 1

basic introduction:

  In some cases, we need to declare (define) method. For example, Person structure: In addition to some external fields (age, name ...), Person structure there are some behaviors such as: can talk, running ... through learning, you can also do arithmetic. Then you should use the method to complete.

  Method Golang is acting on the specified data type (i.e.,: specified data type and binding), thus defined types can have methods, not just struct


Declaration and a call to:

type A struct {
  Num int
}

func (A) test () {
  fmt.Println (a.Num)
}

The syntax description of the above:

1) func (a A) test () {} represents the structure A method for a method named test
2) (A A) is reflected in test methods A and the type of the binding of

for example:

type Person struct {
  Name string
}

// bind to a method of the type Person
FUNC (Person P) Test () {
  fmt.Println ( "Test () name =", p.Name)
}

type Dog struct {

}

func main() {

  the p-the Person var
  p.Name = "tom"
  p.test () method call //

  // The following are the wrong way to use
  var Dog Dog
  dog.test ()
  the Test ()

}

Description of the code above to do:

1) test method and Person type binding

2) test method can be called only by Person types of variables, but can not be called directly, can not be invoked using other types of variables.

3) func (p Person) test () {} ... p represents the Person variable called, p is its copy. And the function parameter passing is very similar.

4) p The name, specified by the programmer, not fixed. For example, it can also be modified to person.


type Person struct {
  Name string
}

// bind to a method of the type Person
FUNC (Person P) Test () {
  p.Name = "Jack"
  fmt.Println ( "Test () name =", p.Name) // output Jack
}

func main() {

  the Person P var
  p.Name = "tom"
  p.test () // call the method
  fmt.Println ( "main () p.Name = ", p.Name) // output tom

}

Getting Started: 

1) adding to speak method Person structure, the output is a good xxx

Case:

type Person struct {
  Name string
}

FUNC (the Person P) Speak () {
  fmt.Println (p.Name, "is a Goodman")
}

func main() {

  var p Person
  p.Name = "tom"
  p.speak()
}

2) adding to the structure jisuan Person method, may be calculated from the result of 1000 + .. + 1

Case:

type Person struct {
  Name string
}

FUNC (the Person P) jisuan () {
  RES: = 0
  for I: =. 1; I <= 1000; I ++ {
    RES = I +
  }
  fmt.Println (p.Name, "result of calculation is =", RES)
}

func main() {

  var p Person
  p.Name = "tom"
  p.jisuan()
}

3) adding to the structure jisuan2 Person method, which may receive a number n, calculated from the result of 1 + .. + n

Case:

type Person struct {
  Name string
}

FUNC (the Person P) jisuan2 (n-int) {
  RES: = 0
  for I: =. 1; I <= n-; I ++ {
    RES = I +
  }
  fmt.Println (p.Name, "result of calculation is =", res )
}

func main() {

  var p Person
  p.Name = "tom"
  p.jisuan2(20)
}

4) adding to the structure getSum Person method, may be calculated and the two numbers, and returns the result.

Case:

type Person struct {
  Name string
}

func (p Person) getSum(n1 int, n2 int) int {
  return n1 + n2
}

func main() {

  var p Person
  p.Name = "tom"
  res := p.getSum(10, 20)
  fmt.Println("res=", res)
}

Call 5) method

  p.speak()
  p.jisuan()
  p.jisuan2(20)
  res := p.getSum(10,20)
  fmt.Println("res=", res)

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11408117.html