go里面对象对象案例-封装

1.案例代码

package main
import "fmt"
type Account struct {
	 AccountNo string
	 Pwd string
	 Money float64
}
//查询账号
func (account * Account ) Query () {
	info:=fmt.Sprintf("账号为[%v]的用户,所存在的钱为[%0.2f]",account.AccountNo,account.Money);
	fmt.Println(info);
}
//账号存储钱
func (account * Account )InsertMoney(money float64,pwd string) {
	if account.Pwd != pwd {
		fmt.Println("密码不对");
		return;
	}
	account.Money += money;
}
//取钱
func (account * Account ) DrawMoney(money float64){
	account.Money = account.Money - money;
}
func main() {
 account:=Account{AccountNo: "sh001",Pwd: "66666",Money: 100.0};
 account.InsertMoney(200,"66666");
 account.Query();
 account.DrawMoney(90.2);
 account.Query();
}

  

原文地址:https://www.cnblogs.com/zh718594493/p/14022355.html