龙空技术网

golang极速嵌入式Linux应用开发(三)-go方法与类成员函数

嵌入式小美老师 136

前言:

而今兄弟们对“怎么调用类的成员函数”大概比较关切,我们都需要剖析一些“怎么调用类的成员函数”的相关资讯。那么小编同时在网上汇集了一些有关“怎么调用类的成员函数””的相关内容,希望朋友们能喜欢,同学们一起来了解一下吧!

1、go方法与类成员函数

方法其实就是一个函数,在func这个关键字和方法名中间加入了一个特殊的接收器类型。接收器可以是结构体类型或者是非结构体类型。接收器是可以在方法的内部访问的。格式定义如下:

func (t Type) methodName(parameter list) {}

因为Go中没有类class的概念,所以要给自定义的类型添加成员函数就无法像C++一样写在类定义中,在Go中只能使用方法这一特性变相的实现成员函数和多态:

//定义类型type Employee struct {    name string    age  int}//添加值接收器的方法func (e Employee) changeName(newName string) {    e.name = newName}//添加指针接收器的方法func (e *Employee) changeAge(newAge int) {    e.age = newAge}func main() {    e := Employee{        name: "Mark Andrew",        age:  50,    }    fmt.Printf("Employee name before change: %s", e.name)//调用属性    e.changeName("Michael Andrew")//调用值接收器,无法改变属性值    fmt.Printf("\nEmployee name after change: %s", e.name)    fmt.Printf("\n\nEmployee age before change: %d", e.age)    (&e).changeAge(51)//调用指针接收器    fmt.Printf("\nEmployee age after change: %d", e.age)}

注意:为了在一个类型上定义一个方法,方法的接收器类型定义和方法的定义应该在同一个包中,其实就是_类定义和成员函数定义要放在一起,很好理解_。

2、接口

在面向对象的领域里,接口一般这样定义:接口定义一个对象的行为。接口只指定了对象应该做什么(抽象定义),至于如何实现这个行为,则由对象本身去确定(实现细节)。

嵌入式物联网需要学的东西真的非常多,千万不要学错了路线和内容,导致工资要不上去!

无偿分享大家一个资料包,差不多150多G。里面学习内容、面经、项目都比较新也比较全!某鱼上买估计至少要好几十。

点击这里找小助理0元领取:加微信领取资料

2.1、接口的定义

在Go语言中,接口就是方法签名(Method Signature)的集合。当一个类型实现了接口中的所有方法,我们称它实现了该接口,这与面向对象编程(OOP)的说法很类似。我们可以用接口来实现C++面向对象的多态

//interface definitiontype interfaceName interface {      methodName(parameter list)    //...}

使用示例如下:

//定义interfacetype Describer interface {      Describe()}//定义类型,可以当作C++类type Person struct {      name string    age  int}type Address struct {    state   string    country string}//使用值接收器实现接口func (p Person) Describe() {    fmt.Printf("%s is %d years old\n", p.name, p.age)}//使用指针接收器实现接口func (a *Address) Describe() { // 使用指针接受者实现    fmt.Printf("State %s Country %s", a.state, a.country)}func main() {      var d1 Describer//定义接口类型变量,类似C++基类    p1 := Person{"Sam", 25}    d1 = p1 //赋值为Person类型的对象,因为该对象已经实现接口,类似C++父类指针赋值子类对象    d1.Describe()//调用成员函数    var d2 Describer    a := Address{"Washington", "USA"}    /* 如果下面一行取消注释会导致编译错误:       cannot use a (type Address) as type Describer       in assignment: Address does not implement       Describer (Describe method has pointer       receiver)    */    //d2 = a    d2 = &a // 这是合法的,因为Address 类型的指针实现了 Describer 接口    d2.Describe()}

我们可以把接口看作内部的一个元组 (type, value)

type是接口底层的具体类型(Concrete Type),可以通过v.(type)获取类型value 是具体类型的值,可以通过v.(T)获取,T为具体的类型,比如int2.2、接口的嵌套

Go 语言没有提供继承机制,但可以通过嵌套其他的接口,创建一个新接口:

type SalaryCalculator interface {      DisplaySalary()}type LeaveCalculator interface {      CalculateLeavesLeft() int}//嵌套两个interface,类似C++继承两个基类type EmployeeOperations interface {      SalaryCalculator    LeaveCalculator    //可以再加新的方法接收器}type Employee struct {      firstName string    lastName string    basicPay int    pf int    totalLeaves int    leavesTaken int}//实现接口方法func (e Employee) DisplaySalary() {      fmt.Printf("%s %s has salary $%d", e.firstName, e.lastName, (e.basicPay + e.pf))}func (e Employee) CalculateLeavesLeft() int {      return e.totalLeaves - e.leavesTaken}

转载自:嵌入式应用研究院

文章来源于golang极速嵌入式Linux应用开发(三)-go方法与类成员函数

原文链接:

标签: #怎么调用类的成员函数