The Object Oriented Method 2

Call mass participation mechanisms and principles of the method :( important!)

Description: Method of calls and mass participation mechanisms and function basically the same, not the same place is variable method call, we will call the method, as also the argument passed to the method. Let us illustrate:

Case 1: The method of execution is shown in front getSum explained +

Description:

1) In adopting a variable to call the method call mechanism and function the same.
2) not the same place, call the method when the variable, the variable itself is also passed as an argument to which way to go. (If the variable is a value type, a value copied, if the variable is a reference type, then the address copy)


Case 2: Write a program, the following:

1) a declaration structure Cirle, field radius

2) a method declaration area and Cirle binding, it can return to the area.

3) Tip: Draw process area to perform + Description

 

Depth analysis of the methods used:

Method declarations (defined)

func (recevier type) methodName (parameter list) (the returned list) {
  method body
  return Return Value
}

1) a list of parameters: input representation

2) recevier type: indicates that this type of method and type this bind, or the type of method acting type

3) recevier type: type structure may be, may be other types of custom

4) recevier type: is a variable (Example) type type, such as: a variable Person structure (Example)

5) Return a list of values: a value indicating the return may be a plurality of

6) The method of FIG Main: that in order to achieve a certain function block

7) return statement is not required


Methods considerations and discuss the details:

  1) structure type is a value type, in the method call, compliance value type transmission mechanism is a value transfer mode copies

  2) If the programmer wishes the process, modify the value of the variable structure, the structure can be handled by way of a pointer

Case presentation:

type Circle struct {
  radius float64
}

@ In order to improve the efficiency of our method is generally pointer types and structures bound
FUNC (c * Circle) Area2 () float64 {
  // because c is a pointer, and therefore access of its fields are our standard (* c). RADIUS
  // return 3.14 * (* C) .radius * (* C) .radius
  //(*c).radius equivalent c.radius
  c.radius = 10.0
  return c.radius 3.14 * * c.radius
}

func main() {

  Circle C var
  c.radius = 7.0
  // RES2: = (& c) .area2 ()
  RES2: c.area2 = ()
  // bottom optimized compiler (& c) .area2 () equivalent c.ares2 ()
  // because the compiler will automatically add to the C &
  fmt.Println ( "area of =", RES2)
  fmt.Println ( "c.radius =", c.radius) // 10
}

3) A method in Golang action on the specified data type (ie: binding and specified data type), thus defined types can have methods, rather than struct, such as int, float32 so there may be method

Case:
Package Penalty for main
Import (
  "fmt"
)

type integer int

func (i integer) print() {
  fmt.Println("i=", i)
}

// write a method, can change the value of i
FUNC (i * Integer) Change () {
  * i * = i +. 1
}

func main() {
  var i integer = 10
  i.print()
  i.change()
  fmt.Println("i=",i)
}


Access range rule 4) control method, and a function. Method name first letter lowercase, this package can only access method capitalized, can be accessed in this package and other packages.

5) if a type implements the String () This method, then fmt.Println default will call this variable String () output

Case:

type Student struct {
  Name string
  Age int
}

func (stu *Student) String() string {
  str := fmt.Sprintf("Name=[%v] Age=[%v]", stu.Name, stu.Age)
  return str
}

func main() {

  STU: Student = {
  the Name: "tom",
  Age: 20,
  }
  // If you implement * Student String type of method, will automatically call
  fmt.Println (& STU)
}

 

The difference between methods and functions:

1) call in a different way

  Function is called: the function name (argument list)
  is called method: method name variable (argument list)

2) For a normal function, the recipient is a value type, pointer type data can not be transmitted directly

案例:
type Person struct {
  Name string
}

func test01(p Person) {
  fmt.Println(p.Name)
}

func test02(p *Person) {
  fmt.Println(p.Name)
}

func main() {

  the p-: the Person = { "tom"}
  test01 (the p-) // because the value is copied, it can not pass the address test01 (* p) so written will complain
  test02 (& p) // address because it is a copy, it is not directly value copy, test02 (p) so the error will be written.
}

3) the method (e.g. struct method), and the receiver is a value type, the method can be called directly by a variable of type pointer, which in turn may be the same.

案例:
type Person struct {
  Name string
}

func (p Person) test03 () {// Although the main function by way of the incoming address, but when the value of p is the received copies of the jack so the following is only a method where the value of the main function the value will not change.
  = p.Name "Jack"
  fmt.Println ( "TEST03 () =", p.Name) // Jack
}

func (p * Person) test04 ( ) {// Since a pointer type, it would be a pointer to the received address, the change in the value of the name of the method, the main function is to change the value.
  = p.Name "Mary"
  fmt.Println ( "test04 () =", p.Name) // Mary
}


func main() {

  P: = {the Person "Tom"}
  p.test03 ()
  fmt.Println ( "main () = p.name", p.Name) // Tom

  (P &) .test03 () is passed from the form // address, but the essence remains the value of the copy
  fmt.Println ( "main p.name =", p.Name) // tom

  (the p-&) .test04 ()
  fmt.Println ( "main () p.name =", the p-. name) // Mary
  p.test04 () // equivalent (& p) .test04 () is passed value type from the form, but still essentially copy address
}


to sum up:

  1) regardless of the form of calls, the real decision is the value of copy or copy the address, this method is mainly to see and which type of binding.

  2) If the value and type of binding, such as (p Person), the value is copied, and if the pointer is a type of binding, such as (p * Person), the address is copied.

Guess you like

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