Go 方法与函数区别

栏目: Go · 发布时间: 6年前

内容简介:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZYC88888/article/details/80307008

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZYC88888/article/details/80307008

方法是特殊的函数,定义在某一特定的类型上,通过类型的实例来进行调用,这个实例被叫接收者(receiver)。

函数将变量作为参数: Function1(recv)

方法在变量上被调用: recv.Method1()

接收者必须有一个显式的名字,这个名字必须在方法中被使用。 

receiver_type

叫做 (接收者)基本类型,这个类型必须在和方法同样的包中被声明。

Go 中,(接收者)类型关联的方法不写在类型结构里面,就像类那样;耦合更加宽松;类型和方法之间的关联由接收者来建立。

方法没有和数据定义(结构体)混在一起:它们是正交的类型;表示(数据)和行为(方法)是独立的。

注意: Go语言 不允许为简单的内置类型添加方法 ,所以下面定义的方法是非法的。

package main

import(
  "fmt"
)


func Add(a ,b int){         //函数合法
  fmt.Println(a+b)
}

func (a int) Add (b int){    //方法非法!不能是内置数据类型
  fmt.Println(a+b)
}

合法的方法定义如下:

package main

import(
  "fmt"
)

type myInt int

func Add(a ,b int){             //函数
  fmt.Println(a+b)
}

func (a myInt) Add (b myInt){   //方法
  fmt.Println(a+b)
}

func main() {
        a, b := 3,4
        var aa,bb myInt = 3,4
        Add(a,b)
        aa.Add(bb)
  }

上面的表达式aa.Add称作选择子(selector),它为接收者aa选择合适的Add方法。

“类的”方法

Go 语言不像其它面相对象语言一样可以写个类,然后在类里面写一堆方法,但其实 Go语言的方法 很巧妙的实现了这种效果:我们只需要在普通函数前面加个接受者(receiver,写在函数名前面的括号里面),这样编译器就知道这个函数(方法)属于哪个struct了。例如:

type A struct {
    Name string
}

func (a A)foo()  { //接收者写在函数名前面的括号里面
    fmt.Println("foo")
}

func main() {
    a := A{}
    a.foo() //foo
}

1.对于普通函数,接收者为值类型时,不能将指针类型的数据直接传递,反之亦然。

2.对于方法(如struct的方法),接收者为值类型时,可以直接用指针类型的变量调用方法,反过来同样也可以。

以下为简单示例:

package structTest  
  
//普通函数与方法的区别(在接收者分别为值类型和指针类型的时候)  
//Date:2014-4-3 10:00:07  
  
import (  
    "fmt"  
)  
  
func StructTest06Base() {  
    structTest0601()  
    structTest0602()  
}  
  
//1.普通函数  
//接收值类型参数的函数  
func valueIntTest(a int) int {  
    return a + 10  
}  
  
//接收指针类型参数的函数  
func pointerIntTest(a *int) int {  
    return *a + 10  
}  
  
func structTest0601() {  
    a := 2  
    fmt.Println("valueIntTest:", valueIntTest(a))  
    //函数的参数为值类型,则不能直接将指针作为参数传递  
    //fmt.Println("valueIntTest:", valueIntTest(&a))  
    //compile error: cannot use &a (type *int) as type int in function argument  
  
    b := 5  
    fmt.Println("pointerIntTest:", pointerIntTest(&b))  
    //同样,当函数的参数为指针类型时,也不能直接将值类型作为参数传递  
    //fmt.Println("pointerIntTest:", pointerIntTest(b))  
    //compile error:cannot use b (type int) as type *int in function argument  
}  
  
//2.方法  
type PersonD struct {  
    id   int  
    name string  
}  
  
//接收者为值类型  
func (p PersonD) valueShowName() {  
    fmt.Println(p.name)  
}  
  
//接收者为指针类型  
func (p *PersonD) pointShowName() {  
    fmt.Println(p.name)  
}  
  
func structTest0602() {  
    //值类型调用方法  
    personValue := PersonD{101, "Will Smith"}  
    personValue.valueShowName()  
    personValue.pointShowName()  
  
    //指针类型调用方法  
    personPointer := &PersonD{102, "Paul Tony"}  
    personPointer.valueShowName()  
    personPointer.pointShowName()  
  
    //与普通函数不同,接收者为指针类型和值类型的方法,指针类型和值类型的变量均可相互调用  
}  


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to Probability Theory and Its Applications

An Introduction to Probability Theory and Its Applications

William Feller / Wiley / 1991-1-1 / USD 120.00

Major changes in this edition include the substitution of probabilistic arguments for combinatorial artifices, and the addition of new sections on branching processes, Markov chains, and the De Moivre......一起来看看 《An Introduction to Probability Theory and Its Applications》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具