golang面向对象method

package main

import "fmt"

type Rectangle struct {
	width, height float64
}

func area(r Rectangle) float64 {
	return r.width * r.height
}
func main() {
	r1 := Rectangle{12, 2}
	r2 := Rectangle{9, 4}
	fmt.Println("Area of r1 is:", area(r1))
	fmt.Println("Area of r2 is: ", area(r2))
}

这段代码可以计算出长方形的面积,但是area()不是作为Rectangle的方法实现的,而是将Rectangle的对象作为参数传入函数计算面积的。 

 当需要圆形,正方形,五边形,你想计算他们的面积。area_rectangle,area_circle,area_triangle

并且从概念上来说,面积是形状的一个属性,它是属于这个特定的形状的。method的概念,method是附属在一个给定的类型上,他的语法和函数的声明语法几乎一样,只是在func后面增加一个receiver ,method area()是依赖某个形状,Rectangle.area()的发出者Rectangle,area()是属于Rectangle的方法。

method的语法如下:

func (r RectiverType)funcName(parameters) (results)

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())
}

使用method的时候重要注意几点,虽然method的名字一模一样,method里面可以访问接收者的字段,method通过.访问,就像struct里面访问字段一样

method area()分别属于Rectangle和Circle,于是他们的Receiver就变成了Rectangle的circle,或者area()方法是由Rectangle circle发出的

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

func (b Box) Volume() float64 {
	return b.width * b.height * b.depth
}
func (b *Box) Setcolor(c Color) {
	b.color = c
}
func (bl BoxList) BiggestColor() Color {
	v := 0.00
	k := Color(WHITE)
	for _, b := range bl {
		if bv := b.Volume(); bv > v {
			v = bv
			k = b.color
		}
	}
	return k
}
func (b1 BoxList) PaintItBlack() {
	for i := range b1 {
		b1[i].Setcolor(BLACK)
	}
}
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(), "立方")
	fmt.Println("The color of the last one is ", boxes[len(boxes)-1].color.string())
	fmt.Println("The biggest one is ", boxes.BiggestColor().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.BiggestColor().string())
}

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

color作为byte的别名

定义了一个struct:Box,含有三个字段和一个属性

 Volume定义了接收者为Box,返回Box的容量

SetColor,吧Box的颜色改为C

BiggestColor()定在Boxlist上面,返回list里面容量最大的颜色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值