抽象是一个事物逻辑的处理
package main
import "fmt"
// 定义一个手机号结构体
type Mobile struct {
User string
Number string
Balance float32
}
// 充值
func (mobile *Mobile) Recharge(money float32) {
if money <= 0 {
fmt.Println("充值的金额不正确")
} else {
mobile.Balance += money
}
}
// 查询余额
func (mobile *Mobile) QueryBalance() float32 {
return mobile.Balance
}
func main() {
mobile := Mobile{
User: "宋江",
Number: "18888888888",
Balance: 20.0,
}
mobile.Recharge(30.0)
balance := mobile.QueryBalance()
fmt.Printf("手机号=%v,余额=%v\n", mobile.Number, balance)
}
![](https://img-blog.csdnimg.cn/img_convert/fc5f2eb0d7ce005dd9f258276369573a.png)