Go语言——struct、type、func的综合用法

最近在学golang语言,对于struct、type、func的学习进行了简单的编程练习,代码如下

package main

import (
	"fmt"
)

const (
	WHITE = iota
	BLACK
	BLUE
	RED
	YELLOW
)

//Color 声明Color为byte的别名
type Color byte 

//Box type box
type Box struct {
	len, wid, high float64
	c Color
}

//BoxList box集合,不确定个数,使用动态数组
type BoxList []Box

//Volume 计算box的容量
func (b Box) Volume() float64 {
	return b.len*b.wid*b.high
}

//SetColor 将box的颜色设为特定值
func (b Box) SetColor(c Color) {
	b.c = c
}

//BiggestColor 找boxlist中容量最大的box的颜色
func (boxlist BoxList) BiggestColor() (c Color) {
	v := 0.0
	c = WHITE

	//遍历boxlist,求容量最大的
	for _, b := range boxlist {
		if tmpv := b.Volume(); tmpv > v {
			v = tmpv
			c = b.c
		}
	}

	return
}

//SetBlack 将boxlist中的box颜色都设置为BLACK
func (boxlist BoxList) SetBlack() {
	for i := range boxlist {
		boxlist[i].c = BLACK
	}
}

//PrintBox 打印box
func (b Box) PrintBox() {
	fmt.Println(b.len, b.wid, b.high, b.Volume(), b.c.String())
}

//String 将颜色以字符串形式返回
func (c Color) String() string {
	strings := []string{"WHITE", "BLACK", "BLUE", "RED", "YELLOW"}
	return strings[c]
}

func main() {
	//创建一个boxlist,存储若干box
	boxlist := BoxList{
		Box{1,2,3,RED},
		Box{1,4,2,WHITE},
		Box{4,2,5,BLACK},
		Box{2,4,3,BLUE},
	}

	//打印所有的box
	for i := range boxlist {
		boxlist[i].PrintBox()
	}

	//输出boxlist的大小
	fmt.Println("len of boxlist = ", len(boxlist))
	
	//输出第二个box的颜色
	fmt.Println("color of boxlist[1] = ", boxlist[1].c.String())
	
	//输出boxlist中容量最大的box的颜色
	fmt.Println("biggest color = ", boxlist.BiggestColor())
	
	//将boxlist中的所有box颜色都设置为黑色
	boxlist.SetBlack()
	fmt.Println("将所有box颜色设置为黑色")
	for i := range boxlist {
		boxlist[i].PrintBox()
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值