4.7面向对象

# 选课系统项目中涉及到诸多数据与功能,要求引入面向对象的思想对其进行高度整合
# 1、学校数据与功能整合
# 2、课程数据与功能进行整合
# 3、学生数据与功能进行整合
# 4、讲师数据与功能进行整合
# 5、班级数据与功能进行整合
# ps:不会写的同学,可以先用普通的方式,先把数据与功能都给写好,再考虑基于面向对象的思想进行整合

# 数据部分:
# 校区的名字:如"老男孩上海校区"
# 校区的地址:如"上海虹桥"


# 班级名字
# 班级所在校区
#
# 学生的学校
# 学生的姓名
# 学生的年龄
# 学号
# 学生的性别
#
# 课程名字
# 课程周期
# 课程价格
#
# 老师的名字
# 老师的年龄
# 老师的薪资
# 老师的等级
#
#
# 功能部分:
# 校区创建完毕后,可以为每个校区创建班级
#
# 班级创建完毕后,可以为每个班级创建课程
#
# 学生创建完毕后,学生可以选择班级
#
# 老师创建完毕后,可以为学生打分




#该方法会在对象产生之后自动执行,专门为对象进行初始化操作,可以有任意代码,但一定不能返回非None的值



class School:

    def __init__(self, school_name, school_address):
        self.school_name = school_name
        self.school_address = school_address

    def school_info(self):
        print(f'校区地址:{self.school_address},校区名字:{self.school_name}')

    def classroom_join(self, classroom_address, classroom_name):
        res = Classroom(classroom_address, classroom_name)
        return res


class Classroom:
    classroom = []

    def __init__(self, classroom_address, classroom_name):
        self.classroom_name = classroom_name
        self.classroom_address = classroom_address

    def class_info(self):
        print(f'班级名字:{self.classroom_name},班级所在的校区名字:{self.classroom_address}')

    def add_course(self, course_name, course_date, course_price):
        res = Course(course_name, course_date, course_price)
        return res


class Course:
    def __init__(self, course_name, course_time, course_price):
        self.course_name = course_name
        self.course_time = course_time
        self.course_price = course_price

    def course_info(self):
        print(f'课程名字:{self.course_name},课程周期:{self.course_time}月,课程价格:{self.course_price}')


class Student:
    def __init__(self, student_school, student_name, student_age, student_number, student_gender):
        self.student_school = student_school
        self.student_name = student_name
        self.student_age = student_age
        self.student_number = student_number
        self.student_gender = student_gender

    def student_info(self):
        print(f'学生学校:{self.student_school},学生姓名:{self.student_name},'
              f'学生年龄:{self.student_age},学生学号:{self.student_number}.学生性别:{self.student_gender} ')

    def class_choice(self, student, classroom):
        print(f'{student}选择了{classroom}')

class Teacher:
    def __init__(self, teacher_name, teacher_age, teacher_salary, teacher_grade):
        self.teacher_name = teacher_name
        self.teacher_age = teacher_age
        self.teacher_salary = teacher_salary
        self.teacher_grade = teacher_grade

    def teacher_info(self):
        print(f'老师的名字:{self.teacher_name} ,老师的年龄:{self.teacher_age},'
              f'老师的薪资:{self.teacher_salary},老师的等级:{self.teacher_grade}')

    def get_score(self, student_name, score):
        print(f'{student_name}得到了{score}分')


# 主程序
a = School('上海虹桥老男孩', '上海')
a.school_info()
b = a.classroom_join('上海虹桥', 'python14')
b.class_info()
c = b.add_course('python', 6, 21800)
c.course_info()
d = Student('上海老男孩', 'zcy', 20, '9', '男')
d.student_info()
d.class_choice('zcy', 'python14')
f = Teacher('egon', 20, 30000, '正在相亲的老师')
f.teacher_info()
f.get_score('zcy', 80)

  

原文地址:https://www.cnblogs.com/Tornadoes-Destroy-Parking-Lots/p/12656530.html