class People:
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
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 Teacher(People):
def __init__(self,name,age,sex,job_title):
super(Teacher,self).__init__(name,age,sex) #使用父类的__init__:先找到Teacher的父类,再把Teacher 的self(对象)传给父类进行初始化
self.job_title=job_title
self.course=[]
self.students=[]
class Student(People):
def __init__(self,name,age,sex):
super(Student,self).__init__(name=name,age=age,sex=sex)
self.course=[]
wesley=Teacher('wesley',18,'male','nb')
print(wesley.name,wesley.age,wesley.sex,wesley.job_title)
s1=Student('hh',15,'female')
print(s1.name,s1.age,s1.sex)
python=Course('python','3months',3000)
linux=Course('linux','3months',3000)
wesley.course.append(python) #组合,wesley.course 是python这个对象
wesley.course.append(linux)
s1.course.append(python)
# print(wesley.course)
wesley.students.append(s1)
for obj in wesley.course: #obj 是python,linux 对象
obj.tell_info() #调用对象的方法
wesley 18 male nb
hh 15 female
python 3months 3000
linux 3months 3000