結構體和接口

一、結構體(struct)定義:就是把使用各類數據類型定義的不一樣變量組合起來的高級數據類型函數

package main

import (
    "fmt"
)

type Rect struct {
    width, lenght float64
}

func main() {
    //.形式賦值
    var rect1 Rect
    rect1.width = 100
    rect1.lenght = 200
    fmt.Println(rect1.width * rect1.lenght)
    //Key/value形式賦值
    rect2 := Rect{width: 100, lenght: 200}
    fmt.Println(rect2.width * rect2.lenght)
    //依據結構體內變量順序賦值
    rect3 := Rect{100, 200}
    fmt.Println("Width:", rect3.width, "Length:", rect3.lenght, "Area:", rect3.width*rect3.lenght)
}

結果圖spa

二、結構體做爲參數傳入函數,即對上面求面積封裝爲函數3d

package main

import (
    "fmt"
)

type Rect struct {
    width, length float64
}

func double_are(rect Rect) float64 {
    rect.width *= 2
    rect.length *= 2
    return rect.width * rect.length
}

func main() {
    var rect = Rect{100, 200}
    fmt.Println(double_are(rect))
    fmt.Println("Width:", rect.width, "Length:", rect.length)
}

結果圖指針

三、結構體組合函數,也就是經過限定函數調用者是哪一個結構體,定義在結構體上面的函數叫作方法(結構體不存在內部函數)code

package main

import (
    "fmt"
)

type Rect struct {
    width, length float64
}

func (rect Rect) area() float64 {
    return rect.width * rect.length
}

func main() {
    var rect = Rect{100, 200}
    fmt.Println("Width:", rect.width, "Length:", rect.length, "Area:", rect.area())
}

結構圖blog

四、結構體和指針,用指針來改變結構體內變量值,若是不改變結構體內變量值,方法的限定類型結構體能夠不爲指針類型。使用new來建立結構體指針類型接口

package main

import (
    "fmt"
)

type Rect struct {
    width, length float64
}

//改變Rect內參數值
func (rect *Rect) double_area() float64 {
    rect.width *= 2
    rect.length *= 2
    return rect.width * rect.length
}

//不改變Rect內參數值
func (rect Rect) double_area2() float64 {
    rect.width *= 2
    rect.length *= 2
    return rect.width * rect.length
}

func main() {
    //使用new建立指針類型
    var rect = new(Rect)
    rect.width = 100
    rect.length = 200
    fmt.Println(*rect)
    //方法的限定類型不是指針類型,則不會改變結構體內參數值
    fmt.Println("Width:", rect.width, "Length:", rect.length, "Area:", rect.double_area2())
    //下面有個很奇怪的現象,是會先運行最後的方法,這樣width和length都擴大爲2倍
    fmt.Println("Double Width:", rect.width, "Double Length:", rect.length, "Double Area:", rect.double_area())
    fmt.Println(*rect)
}

結果圖string

五、結構體內嵌類型,在一個結構體內部定義另一個結構體類型成員class

1)、has-a關係,即iPhone有一個Phoneimport

package main

import (
    "fmt"
)

type Phone struct {
    price int
    color string
}

type IPhone struct {
    phone Phone
    model string
}

func main() {
    var p IPhone
    p.phone.price = 5000
    p.phone.color = "Black"
    p.model = "iPhone 5"
    fmt.Println("I have a iPhone")
    fmt.Println("Price:", p.phone.price)
    fmt.Println("Color:", p.phone.color)
    fmt.Println("Model:", p.model)
}

2)is-a關係,即iPhone也是Phone;假設結構體A內部定義了一個內嵌結構體B,那麼A同時也能夠調用全部定義在B上面的方法

package main

import (
    "fmt"
)

type Phone struct {
    price int
    color string
}
 func (phone Phone) ringing() {
     fmt.Println("Phone is ringing...")
 }
type IPhone struct {
    Phone
    model string
}

func main() {
    var p IPhone
    p.price = 5000
    p.color = "Black"
    p.model = "iPhone 5"
    fmt.Println("I have a iPhone:")
    fmt.Println("Price:", p.price)
    fmt.Println("Color:", p.color)
    fmt.Println("Model:", p.model)
    p.ringing()
}

六、接口(interface):接口裏面能夠定義一組方法,任何其餘類型只要可以實現了這些方法就是實現了這個接口

package main

import (
    "fmt"
)

type Phone interface {
    call()
    sendMsg()
}

type NokiaPhone struct {
}

func (nokiaPhone NokiaPhone) call() {
    fmt.Println("I am Nokia,I can call you")
}

func (nokiaPhone NokiaPhone) sendMsg() {
    fmt.Println("I am Nokia,I can send message to you")
}

type IPhone struct {
}

func (iPhone IPhone) call() {
    fmt.Println("I am iPhone,I can call you")
}

func (iPhone IPhone) sendMsg() {
    fmt.Println("I am iPhone,I can send message to you")
}

func main() {
    var phone Phone

    //NokiaPhone必需要實現Phone中全部方法,不然報錯
    phone = new(NokiaPhone)
    phone.call()

    //IPhone必需要實現Phone中全部方法,不然報錯
    phone = new(IPhone)
    phone.call()
}

七、接口還能夠做爲結構體成員

package main

import (
    "fmt"
)

type Phone interface {
    call()
    sales() int
}

type NokiaPhone struct {
    price int
}

func (nokiaPhone NokiaPhone) call() {
    fmt.Println("I am Nokia,I can call you")
}

func (nokiaPhone NokiaPhone) sales() int {
    return nokiaPhone.price
}

type IPhone struct {
    price int
}

func (iPhone IPhone) call() {
    fmt.Println("I am iPhone,I can call you")
}

func (iPhone IPhone) sales() int {
    return iPhone.price
}

type Person struct {
    phones []Phone
    name   string
    age    int
}

func (person Person) total_cost() int {
    var sum = 0
    for _, phone := range person.phones {
        sum += phone.sales()
    }
    return sum
}

func main() {
    var phones = [5]Phone{
        NokiaPhone{price: 350},
        IPhone{price: 5000},
        IPhone{price: 3400},
        NokiaPhone{price: 450},
        IPhone{price: 5000},
    }
    //注意phones要使用切片phones[:]
    var person = Person{name: "Jemy", age: 20, phones: phones[:]}
    fmt.Println(person.name)
    fmt.Println(person.age)
    fmt.Println(person.total_cost())
}

結果圖

相關文章
相關標籤/搜索