4.8---面向对象练习2

Posted zhubincheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.8---面向对象练习2相关的知识,希望对你有一定的参考价值。

 

改写要求:

# 1、基于面向对象代码,扩写Student类
# 2、加入序列化与反序列化操作
# 3、对象之间的关联采用id号
# 4、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象

 

类定义部分:

import uuid
import pickle

class School:
    school_name = "老男孩"

    def __init__(self,nickname,address):
        self.nickname = nickname
        self.address = address
        self.classes = []
        self.id = uuid.uuid4()

    def relate_class(self,class_obj):
        self.classes.append(class_obj.id)

    def save(self):
        with open(f"dbschool\{self.id}.pickle",mode="wb") as f:
            pickle.dump(self,f)


    def tell_class(self):
        print(self.nickname.center(50,"="))
        for class_id in self.classes:
            with open(f"dbclass\{class_id}.pickle", mode="rb") as f:
                class_obj = pickle.load(f)
            class_obj.tell_course()

class Class:
    def __init__(self,name):
        self.name = name
        self.course = None
        self.student = []
        self.id = uuid.uuid4()
        self.school = None

    def relate_course(self,course_obj):
        self.course = course_obj.id
        
    def relate_student(self,student_obj):
        self.student.append(student_obj.id)

    def save(self):
        with open(f"dbclass\{self.id}.pickle",mode="wb") as f:
            pickle.dump(self,f)

    def tell_course(self):
        print("%-10s " %self.name,end=" ")
        with open(f"dbcourse\{self.course}.pickle",mode="rb") as f:
            course = pickle.load(f)
        course.tell_info()


class Course:
    def __init__(self,name,time,cost):
        self.name = name
        self.time = time
        self.cost = cost
        self.id = uuid.uuid4()

    def tell_info(self):
        print("课程名:%-10s 时长:%s,费用:%s" %(self.name,self.time,self.cost))

    def save(self):
        with open(f"dbcourse\{self.id}.pickle",mode="wb") as f:
            pickle.dump(self,f)


class Student:
    def __init__(self,name,age,gender,num):
        self.name = name
        self.age = age
        self.gender = gender
        self.num = num
        self.class_obj = None
        self.id = uuid.uuid4()

    def tell_info(self):
        print("姓名:%-5s 年龄:%s 性别:%s,学号:%-5s"%(self.name,self.age,self.gender,self.num))

    def chose_class(self,class_obj):
        self.class_obj = class_obj.id
        class_obj.student.append(self.id)

    def save(self):
        with open(f"dbstudent\{self.id}.pickle", mode="wb") as f:
            pickle.dump(self, f)

    def tell_class(self):
        print(f"学生【{self.name}】班级信息".center(50,"="))
        with open(f"dbclass\{self.class_obj}.pickle",mode="rb") as f:
            class_obj = pickle.load(f)
        class_obj.tell_course()

 

实例化部分:

# 实例化对象       
stu1 =Student("Jil",18,"mal",1118)
s1 = School("猪猪校区","上海徐汇")
c1 = Class("猪猪基础班")
c2 = Class("猪猪进阶班")
course1 = Course("猪的自我修养","6 months",20000)
course2 = Course("鱼丸捕捉十式","5 months",18000)

# 关联
s1.relate_class(c1)
s1.relate_class(c2)
c1.relate_course(course1)
c2.relate_course(course2)

# 学生选班级
stu1.chose_class(c1)

# 存入文件
s1.save()
c1.save()
c2.save()
course1.save()
course2.save()
stu1.save()

# 查看
s1.tell_class()
c1.tell_course()
stu1.tell_class()

 

以上是关于4.8---面向对象练习2的主要内容,如果未能解决你的问题,请参考以下文章

web代码片段

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

PHP面向对象练习2

PHP面向对象练习

Java面向对象练习