从面向过程到面向对象实例 - Go语言 -- 十安辰

    技术2024-12-29  24

    一、项目需求

    模拟实现基于文本界面的《家庭记账软件》该软件能够记录家庭的收入、支出,并能够打印收支明细表

    二、面向过程实例

    package main import ( "fmt" ) func main() { //声明一个变量,保存接收用户输入的选项 key := "" //声明一个变量,控制是否退出for loop := true //定义账户的余额 [] balance := 10000.0 //每次收支的金额 money := 0.0 //每次收支的说明 note := "" //定义个变量,记录是否有收支的行为 flag := false //收支的详情使用字符串来记录 //当有收支时,只需要对details 进行拼接处理即可 details := "收支\t账户金额\t收支金额\t说 明" //显示这个主菜单 for { fmt.Println("\n-----------------家庭收支记账软件-----------------") fmt.Println(" 1 收支明细") fmt.Println(" 2 登记收入") fmt.Println(" 3 登记支出") fmt.Println(" 4 退出软件") fmt.Print("请选择(1-4):") fmt.Scanln(&key) switch key { case "1": fmt.Println("-----------------当前收支明细记录-----------------") if flag { fmt.Println(details) } else { fmt.Println("当前没有收支明细... 来一笔吧!") } case "2": fmt.Println("本次收入金额:") fmt.Scanln(&money) balance += money // 修改账户余额 fmt.Println("本次收入说明:") fmt.Scanln(&note) //将这个收入情况,拼接到details变量 //收入 11000 1000 有人发红包 details += fmt.Sprintf("\n收入\t%v\t%v\t%v", balance, money, note) flag = true case "3": fmt.Println("本次支出金额:") fmt.Scanln(&money) //这里需要做一个必要的判断 if money > balance { fmt.Println("余额的金额不足") break } balance -= money fmt.Println("本次支出说明:") fmt.Scanln(&note) details += fmt.Sprintf("\n支出\t%v\t%v\t%v", balance, money, note) flag = true case "4": fmt.Println("你确定要退出吗? y/n") choice := "" for { fmt.Scanln(&choice) if choice == "y" || choice == "n" { break } fmt.Println("你的输入有误,请重新输入 y/n") } if choice == "y" { loop = false } default: fmt.Println("请输入正确的选项..") } if !loop { break } } fmt.Println("你退出家庭记账软件的使用...") }

    三、从面向过程到面向对象封装注意注意事项

    面向对象要求减小代码之间的耦合性把原来的事物封装成一个个结构体(对比c++的类)尽量把主逻辑函数简化,将原来的代码块儿封装成一个个函数

    四、重构过后的代码

    封装模块utils.go

    如下:代码具体含义见注释

    package utils import ( "fmt" ) // FamilyAccount 定义结构体 type FamilyAccount struct { //声明一个变量,保存接收用户输入的选项 key string //声明一个变量,控制是否退出for loop bool //定义账户的余额 [] balance float64 //每次收支的金额 money float64 //每次收支的说明 note string //定义个变量,记录是否有收支的行为 flag bool //收支的详情使用字符串来记录 //当有收支时,只需要对details 进行拼接处理即可 details string //显示这个主菜单 } // NewFamilyAccount 编写工厂模式的构造方法,返回一个*FamilyAccount实例 func NewFamilyAccount() *FamilyAccount { return &FamilyAccount{ key: "", loop: true, balance: 10000.0, money: 0.0, note: "", flag: false, details: "收支\t账户金额\t收支金额\t说 明", } } // showDtails 将case1显示明细写成相应的方法 func (this *FamilyAccount) showDtails() { fmt.Println("-----------------当前收支明细记录-----------------") if this.flag { fmt.Println(this.details) } else { fmt.Println("当前没有收支明细... 来一笔吧!") } } // income 将case2登记收入写成一个方法 func (this *FamilyAccount) income() { fmt.Println("本次收入金额:") fmt.Scanln(&this.money) this.balance += this.money // 修改账户余额 fmt.Println("本次收入说明:") fmt.Scanln(&this.note) //将这个收入情况,拼接到details变量 //收入 11000 1000 有人发红包 this.details += fmt.Sprintf("\n收入\t%v\t%v\t%v", this.balance, this.money, this.note) this.flag = true } // pay 将case3登记支出成一个方法 func (this *FamilyAccount) pay() { fmt.Println("本次支出金额:") fmt.Scanln(&this.money) //这里需要做一个必要的判断 if this.money > this.balance { fmt.Println("余额的金额不足") //break } this.balance -= this.money fmt.Println("本次支出说明:") fmt.Scanln(&this.note) this.details += fmt.Sprintf("\n支出\t%v\t%v\t%v", this.balance, this.money, this.note) this.flag = true } // quit将case4退出写成一个方法 func (this *FamilyAccount) quit() { fmt.Println("你确定要退出吗? y/n") choice := "" for { fmt.Scanln(&choice) if choice == "y" || choice == "n" { break } fmt.Println("你的输入有误,请重新输入 y/n") } if choice == "y" { this.loop = false } } //绑定相应的方法 // MainMenu 显示主菜单 func (this *FamilyAccount) MainMenu() { for { fmt.Println("\n-----------------家庭收支记账软件-----------------") fmt.Println(" 1 收支明细") fmt.Println(" 2 登记收入") fmt.Println(" 3 登记支出") fmt.Println(" 4 退出软件") fmt.Print("请选择(1-4):") fmt.Scanln(&this.key) switch this.key { case "1": this.showDtails() case "2": this.income() case "3": this.pay() case "4": this.quit() default: fmt.Println("请输入正确的选项..") } if !this.loop { break } } }

    主函数

    主函数负责创建familyAccount对象,调用对应的方法,如下:

    package main import ( "chapter12/familyaccount/utils" "fmt" ) func main() { fmt.Println("先创建一个familyaccount对象") //调用主菜单函数 utils.NewFamilyAccount().MainMenu() }
    Processed: 0.009, SQL: 9