天天看點

golang極速嵌入式Linux應用開發(三)-go方法與類成員函數

作者:嵌入式小美老師

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元領取:加微信領取資料

golang極速嵌入式Linux應用開發(三)-go方法與類成員函數

2.1、接口的定義

在Go語言中,接口就是方法簽名(Method Signature)的集合。當一個類型實作了接口中的所有方法,我們稱它實作了該接口,這與面向對象程式設計(OOP)的說法很類似。我們可以用接口來實作C++面向對象的多态。

//interface definition
type interfaceName interface {  
    methodName(parameter list)
    //...
}
           

使用示例如下:

//定義interface
type 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為具體的類型,比如int

2.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方法與類成員函數

原文連結:https://mp.weixin.qq.com/s/AwLi5w1dVtr41jY1jUxkqA