Python 组合

继承是 什么是什么的关系

老师有课程: 有的关系是组合
学生有生日 有的关系是组合

老师增加课程属性=课程对象

#_*_coding:utf-8_*_
class People:
    school='luffycity'

    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 teach(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('alex',18,'male',10,3000,)
# teacher2=Teacher('egon',28,'male',30,3000,)
# python=Course('python',3000,'3mons')
# linux=Course('linux',2000,'4mons')

#python课程名字
# print(python.course_name)

#老师教python课程   老师的对象定制一个课程的属性(course就是属性) ,让这个属性指向另外实例的对象
# teacher1.course=python
# teacher2.course=python

# print(python)
# print(teacher1.course)
# print(teacher2.course)
# print(teacher1.course.course_name)
# print(teacher2.course.course_name)
# 
#让老师有一个属性,去指向实例对象
#
# teacher1.course.tell_info()

# student1=Student('张三',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)


#学生
student1=Student('张三',28,'female','08:30:00')
#日期
d=Date(1988,4,20)
#python课程
python=Course('python',3000,'3mons')

#bird就是属性 
student1.birh=d
student1.birh.tell_info()

student1.course=python

student1.course.tell_info()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟伟哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值