Go 面向对象编程应用
#### Go 面向对象编程应用
前面学习了很多的基础知识,这一节来实际写一个小案例:
涉及到的知识:
1. 数组的基本使用
2. 结构体
3. 切片
4. 方法
5. 循环
6. 函数返回值(命名返回值,普通返回值)
备注: 大家也可以在上面加入从终端获取用户信息,将方法动作根据终端选择绑定
---
案例1: 班级管理系统
1. 默认3个班级
2. 学生有姓名,年龄,所在班级属性
3. 学生可以选择加入班级,切换班级
package main
import (
"errors"
"fmt"
)
// 学生信息
type Student struct {
// 名称
Name string
// 年龄
Age int
// 所属班级
Class string
}
// 班级信息
type Class struct {
// 班级名称
Name string
// 班级中所有的学生
Students []Student
}
// 班级列表
var CLASS = [3]Class{
{Name: "Music"},
{Name: "Math"},
{Name: "English"},
}
// 显示学生的信息: 名称,年龄,所属班级
func (s *Student) showInfo() {
fmt.Printf("Name: %s,Age: %d,Class: %s\n", s.Name, s.Age, s.Class)
}
// 加入班级方法
func (s *Student) joinClass(class string) {
// 加入班级前判断是否已经加入班级
//TODO 如果初始化学生实例设置了class 则不能加入任何班级
if s.Class != "" {
return
}
var err error
// 要加入班级的索引
var classIndex int
classIndex, err = searchClassByName(class)
// 根据查找的值处理是否加入班级
if err == nil {
// 更改学生的班级
s.Class = class
CLASS[classIndex].Students = append(CLASS[classIndex].Students, *s)
} else {
// 要加入的班级不存在
fmt.Println("class is not exist,join failed")
}
}
// 学生换班级方法
func (s *Student) changeClass(class string) {
// 换班级前先检查是否已经在某个班级
if s.Class == class {
fmt.Println("当前学生已存在目标班级")
return
}
var err error
var index int // 目标班级的索引
index, err = searchClassByName(class)
if err != nil {
// 查找的班级不存在
fmt.Println("查找的班级不存在")
return
}
// 将学生加入到新的班级学生列表中
CLASS[index].Students = append(CLASS[index].Students, *s)
// 学生原来的班级索引
oldStudentClassIndex, _ := searchClassByName(s.Class)
// 查找学生的索引
studentIndex := searchStudentByName(oldStudentClassIndex, s.Name)
// 使用新的切片存储
var newStudents = make([]Student, 0)
if studentIndex != 0 {
newStudents = append(CLASS[index].Students[:studentIndex], CLASS[index].Students[index+1:]...)
}
CLASS[oldStudentClassIndex].Students = newStudents
// 加入新班级后将学生的班级更新
s.Class = class
}
// 根据班级名称查找班级的索引
func searchClassByName(class string) (index int, err error) {
for i, val := range CLASS {
if val.Name == class {
index = i
return
}
}
err = errors.New("class is not exist")
return
}
// 根据学生名称和班级索引查找学生索引
// 此函数由从学生的信息上获取class 名称,再通过class 名称查找到class 的索引,所以这个函数没有错误
// TODO 如果调用的场景不是上述描述的应该会有错误
func searchStudentByName(class int, name string) (index int) {
for i, val := range CLASS[class].Students {
if val.Name == name {
// 查找到了学生
index = i
}
}
return
}
// 查看所有班级的信息
func showClassInfo() {
for _, val := range CLASS {
fmt.Println("当前班级:", val.Name)
for _, stu := range val.Students {
fmt.Println("当前班级有学生:", stu)
}
}
}
// 初始化一个学生
func newStudent(name string, age int) Student {
s := Student{
Name: name,
Age: age,
}
return s
}
func main() {
// xiaoMing 加入Music
var xiaoMing Student
xiaoMing = newStudent("xiaoMing", 20)
xiaoMing.joinClass("Music")
xiaoMing.showInfo()
// xiaoHu加入Music
var xiaoHu Student
xiaoHu = newStudent("xiaoHu", 18)
xiaoHu.joinClass("Music")
xiaoHu.showInfo()
// xiaoHong 加入Math
var xiaoHong Student
xiaoHong.Name = "xiaoHong"
xiaoHong.Age = 22
xiaoHong.joinClass("Math")
xiaoHong.showInfo()
// xiaoHua 加入English
xiaoHua := Student{
Name: "xiaoHua",
Age: 23,
}
xiaoHua.joinClass("English")
xiaoHua.showInfo()
// xiaoJia 加入不存在的班级Paint
xiaoJia := Student{
Name: "xiaoJia",
Age: 21,
}
xiaoJia.joinClass("Paint")
xiaoJia.showInfo()
showClassInfo()
fmt.Println("xiaoHu change class Math")
xiaoHu.changeClass("Math")
showClassInfo()
xiaoHu.showInfo()
fmt.Println("xiaoHua change class Math")
xiaoHua.changeClass("Math")
showClassInfo()
xiaoHua.showInfo()
}
个人微信公众号会发布最新文章,欢迎关注一同交流学习

每天进步一点点!加油

浙公网安备 33010602011771号