标签:atm print lan 余额 port ring amount 账户 存取款
package main import "fmt" /* @ATM(闭包练习) ·写一个Atm(函数),返回存款,取款两个内层函数 ·存款,取款两个函数,都以一个金额为参数,返回存取之后的余额 ·将各自的余额存在函数闭包中 ·比尔,马云,你任意存取之后,输出各自余额 */ func Atm(name string) (func(amount int) (ballance int), func(amount int) (ballance int)) { /* 内存函数的账户姓名 内存函数的账户余额 */ var accountName string var accountBallance int accountName = name /*欢迎语*/ fmt.Println("欢迎你", accountName) /*定义存款函数*/ saveFunc := func(amount int) (ballance int) { accountBallance += amount return accountBallance } /*定义取款函数*/ drawFunc := func(amount int) (ballance int) { accountBallance -= amount return accountBallance } /*返回存取款函数*/ return saveFunc, drawFunc } func main() { /*调用闭包函数得到各自的存取款函数副本*/ billSave, billDraw := Atm("Bill") jackSave, jackDraw := Atm("Jack") diorsSave, diorsDraw := Atm("Diors") /*各自存款*/ billBallance := billSave(1000000) jackBallance := jackSave(500000) diorsBallance := diorsSave(999) /*各自取款*/ diorsBallance = diorsDraw(500) billBallance = billDraw(300000) diorsBallance = diorsDraw(300) jackBallance = jackDraw(400000) diorsBallance = diorsDraw(100) /*打印余额*/ fmt.Println(billBallance,jackBallance,diorsBallance) }
输出:
欢迎你 Bill 欢迎你 Jack 欢迎你 Diors 700000 100000 99
标签:atm print lan 余额 port ring amount 账户 存取款
原文地址:https://www.cnblogs.com/yunweiqiang/p/11802517.html