Python-3 组合

废话不多说,直接上代码

class People:
    school = 'liffycity'

    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex


class Teacher(People):
    def __init__(self, name, age, sex, level, salary):
        super().__init__(name, age, sex)
        self.level = level
        self.salary = salary

    def teacher(self):
        print('%s is teaching' % self.name)


class Student(People):
    def __init__(self, name, age, sex, class_time):
        super().__init__(name, age, sex)

        self.class_time = class_time

    def learn(self):
        print('%s is learning' % self.name)


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

    def tell_info(self):
        print('课程名<%s> 课程价钱<%s> 课程周期<%s>' % (self.course_name, self.course_price, self.course_period))


class Date:
    def __init__(self, year, mon, day):
        self.year = year
        self.mon = mon
        self.day = day

    def tell_info(self):
        print('%s-%s-%s' % (self.year, self.mon, self.day))


teacher1 = Teacher('winnie', 18, 'female', 10, 300)
teacher2 = Teacher('allen', 18, 'male', 30, 300)
python = Course('python', 3000, '3mons')
linux = Course('linux', 2000, '4mons')

# 组合使用(老师教什么课)
teacher1.course = python  # teacher1教python课程,给teacher1创建一个属性course,将python的课程直接赋值给teacher1的course这个属性
teacher2.course = linux  # teacher1教python课程,给teacher1创建一个属性course,将python的课程直接赋值给teacher1的course这个属性

print(teacher1.course.course_name)  # python
print(teacher2.course.course_name)  # linux
teacher1.course.tell_info()  # 课程名<python> 课程价钱<3000> 课程周期<3mons>

# 组合使用(学生上什么课)
print('==========================')
student1 = Student('vivian', 28, 'female', '08:30:00')
student1.course1 = python
student1.course2 = linux

student1.course1.tell_info()
student1.course2.tell_info()
student1.courses = []
student1.courses.append(python)
student1.courses.append(linux)

# 组合使用(学生的生日、学生的课程)
print('==========================')
student1 = Student('vivian', 28, 'female', '08:30:00')
d = Date(1991, 12, 2)
python = Course('pyhton', 3000, '3mons')

student1.birth = d
student1.birth.tell_info()

student1.courses = python

student1.courses.tell_info()

结果:
python
linux
课程名<python> 课程价钱<3000> 课程周期<3mons>
==========================
课程名<python> 课程价钱<3000> 课程周期<3mons>
课程名<linux> 课程价钱<2000> 课程周期<4mons>
==========================
1991-12-2
课程名<pyhton> 课程价钱<3000> 课程周期<3mons>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值