Golang basis _08- method method

table of Contents

@

Statements and methods of use

receiver

  • Go in, although not class, but still there is method
  • Is achieved by the receiver display shows a type of a combination
type A struct {
    Name string
}
type B struct {
    Name string
}

func main(){
    a := A{}
    a.Print()

    b := B{}
    b.Print()
}
func (a A) Print(){
    fmt.Println("A")
}
func (b B) Print(){
    fmt.Println("B")
}
//就像重载一样,但是不是(a A)(b B)就是receiver
/*
> Output:
command-line-arguments
A
B
*/

Method overloading does not exist

  • Absence of method overloading, following a case like this does not exist
func (a A) Print(){
    fmt.Println("A")
}
func (a A) Print(b int){
    fmt.Println("A")
}
  • Only the type defined in the same way as a package
  • receiver can be a value or a pointer type
type A struct {
    Name string
}
type B struct {
    Name string
}

func main(){
    a := A{}
    a.Print()
    fmt.Println(a.Name)

    b := B{}
    b.Print()
    fmt.Println(b.Name)
}
//指针传递
func (a *A) Print(){
    a.Name="AA"
    fmt.Println("A")
}
//值传递
func (b B) Print(){
    b.Name="BB"
    fmt.Println("B")
}
/*
> Output:
command-line-arguments
A
AA
B
<空格>
> Elapsed: 6.294s
*/
  • Can use the value or pointer to call the method, a.Print()the compiler will automatically transitioned

Type aliases and methods

  • go language requires mandatory conversion, but also because of this
  • Alias ​​does not have the type of method of the underlying type of incidental
type TZ int
func main(){
    var a TZ
    a.Print()
}
func (a *TZ) Print(){
    fmt.Println("TZ")
}
/*
> Output:
command-line-arguments
TZ
*/

Method Value与Method Expression

  • In a sense, the function is syntactic sugar, because the receiver is actually received by the first method parameter (Method Value vs. Method Expression)
    //method value
    a.Print()
    //method expression
    (*TZ).Print(&a)

Both the output of the same

  • If the outer structure and the embedded structure is present method with the same name, the priority call an external configuration of a method of

Method name conflicts with field access

  • Method can be called the structure of the non-public field (private), the function can be accessed in main (), is the current authority of this package are visible
type A struct{
    name string
}
func main(){
    a := A{}
    a.Print()
    fmt.Println(a.name)
}
func (a *A) Print(){
    a.name="123"
    fmt.Println(a.name)
}
/*
> Output:
command-line-arguments
123
123
*/

example

type A int
func (a *A) Increase(){
    *a += 100
}
func (a *A)Increase2(num A) {
    *a += num
}
func main(){
//显示类型声明,不要用a := 0,这样就是int了
    var a A
    a=0
    a.Increase()
    fmt.Println(a)
    a.Increase2(100)
    fmt.Println(a)
}
/*
> Output:
command-line-arguments
100
200
*/

tips

The difference between the methods and functions

Methods: func (a A) add(num int) int {}
Functions:func add(num1 int,num2 int) int {}

The difference between methods and functions, methods, after the func keyword is a recipient rather than a function name, the recipient may be a type of their own definition, this type can be a struct, interface, and even we can redefine the basic data types.

Guess you like

Origin www.cnblogs.com/leafs99/p/golang_basic_08.html