天天看点

Golang 抽象实现笔记

抽象是一个事物逻辑的处理

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)
}
           
Golang 抽象实现笔记

继续阅读