GO方法笔记

package main

import (

    "fmt" "math"

)

type Rectangle struct {

    width, height float64

}

type Circle struct {

    radius float64

}

func (r Rectangle) area() float64 {

    return r.width * r.height

}

func (c Circle) area() float64 {

    return c.radius * c.radius * math.Pi

}

func main() {

    r1 := Rectangle{12, 2}

    r2 := Rectangle{9, 4}

    c1 := Circle{10}

    c2 := Circle{25}

    fmt.Println("Area of r1 is: ", r1.area())

    fmt.Println("Area of r2 is: ", r2.area())

    fmt.Println("Area of c1 is: ", c1.area())

    fmt.Println("Area of c2 is: ", c2.area())

}

输出结果为:

Area of r1 is: 24

Area of r2 is: 36

Area of c1 is: 314.1592653589793

Area of c2 is: 1963.4954084936207        

使用method的时候重要注意以下几点。

        ●  虽然method的名字一模一样,但是如果接收者不一样,那么method就不一样。

        ●  method里面可以访问接收者的字段。

        ●  调用method通过访问,就像struct里面访问字段一样。

package main

import "fmt"

const (

    WHITE = iota

    BLACK

    BLUE

    RED

    YELLOW

)

type Color byte type Box struct {

    width, height, depth float64

    color Color

}

type BoxList []Box // a slice of boxes

/* Box Volume() SetColor(c Color) */

func (b Box) Volume() float64 {

    return b.width * b.height * b.depth

}

func (b *Box) SetColor(c Color) {

    b.color = c

}

/* BoxList BiggestsColor() PaintItBlack() */

func (bl BoxList) BiggestsColor() Color {

    v := 0.00

    k := Color(WHITE) // k := byte(WHITE)

    for _, b := range bl {

        if b.Volume() > v {

            v = b.Volume() k = b.color

        }

    }

    return k

}

func (bl BoxList) PaintItBlack() {

    for i, _ := range bl {

        bl[i].SetColor(BLACK)

    }

}

/* Color String() */

func (c Color) String() string {

    strings := []string{"WHITE", "BLACK", "BLUE", "RED", "YELLOW"} return strings[c]

}

func main() {

    boxes := BoxList{ Box{4, 4, 4, RED},

    Box{10, 10, 1, YELLOW},

    Box{1, 1, 20, BLACK},

    Box{10, 10, 1, BLUE},

    Box{10, 30, 1, WHITE},

    Box{20, 20, 20, YELLOW},

    }

    fmt.Printf("We have %d boxes in our set\n", len(boxes)) fmt.Println("The volume of the first one is", boxes[0].Volume(), "cm3")

    fmt.Println("The color of the last one is", boxes[len(boxes)-1].color.String()) fmt.Println("The biggest one is", boxes.BiggestsColor().String())

    fmt.Println("Let`s paint them all black") boxes.PaintItBlack()

    fmt.Println("The color of the second one is", boxes[1].color.String()) fmt.Println("Obviously, now, the biggest one is",

    boxes.BiggestsColor().String())

}

输出结果为:

We have 6 boxes in our set

The volume of the first one is 64 cm3

The color of the last one is YELLOW

The biggest one is YELLOW

Let`s paint them all black

The color of the second one is BLACK

Obviously, now, the biggest one is BLACK

 上面的代码通过const定义了一些常量,然后定义了一些自定义类型。

    ● Color作为byte的别名。

    ● 定义了一个struct:Box,含有三个长宽高字段和一个颜色属性。

    ● 定义了一个slice:BoxList,含有Box。然后以上面的自定义类型为接收者定义了一些method。

    ● Volume()定义了接收者为Box,返回Box的容量。

    ● SetColor(cColor),把Box的颜色改为c。

    ● BiggestsColor()定在在BoxList上面,返回list里面容量最大的颜色。

    ● PaintItBlack()把BoxList里面所有Box的颜色全部变成黑色。

    ● String()定义在Color上面,返回Color的具体颜色(字符串格式)。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容