python第28天作业

1、练习上课作业讲解的面向对象代码

2、基于上课作业讲解的面向对象代码,扩写Student类

class School:
    school_name = 'ZJNU'

    def __init__(self, nickname, addr):
        self.nickname = nickname
        self.addr = addr
        self.classes = []

    def related_class(self, class_obj):
        self.classes.append(class_obj)

    def tell_class(self):  # 改
        # 打印的班级的名字
        print(self.nickname.center(60, '='))
        # 打印班级开设的课程信息
        for class_obj in self.classes:
            class_obj.tell_course()


class Class:
    def __init__(self, name):
        self.name = name
        self.course = None
        self.students= []

    def related_course(self, course_obj):
        self.course = course_obj

    def add_student(self,stu_id):
        self.students.append(stu_id)

    def tell_course(self):
        print('%s' % self.name, end=" ")
        self.course.tell_info()  # 打印课程的详细信息


class Course:
    def __init__(self, name, period, price):
        self.name = name
        self.period = period
        self.price = price

    def tell_info(self):
        print('<课程名:%s 周期:%s 价钱:%s>' % (self.name, self.period, self.price))


class Student:
    """ 学生的学校
     学生的姓名
     学生的年龄
     学号
     学生的性别"""

    def __init__(self, name, age, student_number, gender,classname = '散人'):
        self.name = name
        self.age = age
        self.student_number = student_number
        self.gender = gender
        self.classname = classname

    def pr_info(self):
        print('新学生加入!')
        print("姓名:%-5s 年龄:%s 性别:%s 学号:%-5s 班级:%s"
              % (self.name, self.age, self.student_number, self.gender,self.classname))
        return self.name

    def add_class(self,class_name):
        self.classname = class_name
        print('加入班级成功')
        print("姓名:%-5s 年龄:%s 性别:%s 学号:%-5s 班级:%s"
              % (self.name, self.age, self.student_number, self.gender,self.classname))


# 创建学校
school_obj1 = School('浙江师范大学','金华')
school_obj2 = School('杭州电子科技大学大学','杭州')

# 创建班级
class_obj1 = Class('计算机科学与技术15')
class_obj2 = Class('软件工程15')

# 创建课程
course_obj1=Course('数据结构','6mons',600)
course_obj2=Course('编译原理','5mons',450)

# 创建学生
stu1_obj = Student('Lance',18,1558,'male')
stu2_obj = Student('lili',18,1238,'female')
stu3_obj = Student('pipi',19,1428,'male')

# 学校关联专业班级
school_obj1.related_class(class_obj1)
school_obj2.related_class(class_obj2)

# 班级关联课程
class_obj1.related_course(course_obj1)
class_obj2.related_course(course_obj2)

# 学生关联班级
stu_name1 = stu1_obj.pr_info()
stu_name2 = stu1_obj.pr_info()
stu_name3 = stu2_obj.pr_info()

stu1_obj.add_class(stu_name1)
stu2_obj.add_class(stu_name2)
stu3_obj.add_class(stu_name3)

school_obj1.tell_class()
school_obj2.tell_class()

3、加入序列化与反序列化操作

4、对象之间的关联采用id号

5、可以通过id找到对应的文件,然后从文件中反序列化出执行的学校、班级、课程、学生对象

posted @ 2020-04-09 00:44  Lance_王  阅读(154)  评论(0编辑  收藏  举报