标签:对象关联 pre 理解 自己的 对象 fine 定义 自己 使用
class OldboyPeople:
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class OldboyStudent(OldboyPeople):
def __init__(self, name, age, sex, stu_id):
OldboyPeople.__init__(self, name, age, sex)
self.stu_id = stu_id
def choose_course(self):
print('%s is choosing course' % self.name)
class OldboyTeacher(OldboyPeople):
def __init__(self, name, age, sex, level):
OldboyPeople.__init__(self, name, age, sex)
self.level = level
def score(self, stu, num):
stu.score = num
print('老师[%s]为学生[%s]打分[%s]' % (self.name, stu.name, num))
stu1 = OldboyStudent('tank', 19, 'male', 1)
tea1 = OldboyTeacher('nick', 18, 'male', 10)
stu1.choose_course()
tank is choosing course
tea1.score(stu1, 100)
老师[nick]为学生[tank]打分[100]
print(stu1.__dict__)
{'name': 'tank', 'age': 19, 'sex': 'male', 'stu_id': 1, 'score': 100}
class Course:
def __init__(self, name, period, price):
self.name = name
self.period = period
self.price = price
def tell_info(self):
msg = """
课程名:%s
课程周期:%s
课程价钱:%s
""" % (self.name, self.period, self.price)
print(msg)
class OldboyPeople:
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class OldboyStudent(OldboyPeople):
def __init__(self, name, age, sex, stu_id):
OldboyPeople.__init__(self, name, age, sex)
self.stu_id = stu_id
def choose_course(self):
print('%s is choosing course' % self.name)
class OldboyTeacher(OldboyPeople):
def __init__(self, name, age, sex, level):
OldboyPeople.__init__(self, name, age, sex)
self.level = level
def score(self, stu, num):
stu.score = num
print('老师[%s]为学生[%s]打分[%s]' % (self.name, stu.name, num))
# 创造课程
python = Course('python全栈开发', '5mons', 3000)
python.tell_info()
课程名:python全栈开发
课程周期:5mons
课程价钱:3000
linux = Course('linux运维', '5mons', 800)
linux.tell_info()
课程名:linux运维
课程周期:5mons
课程价钱:800
# 创造学生与老师
stu1 = OldboyStudent('tank', 19, 'male', 1)
tea1 = OldboyTeacher('nick', 18, 'male', 10)
# 将学生、老师与课程对象关联/组合
stu1.course = python
tea1.course = linux
stu1.course.tell_info()
课程名:python全栈开发
课程周期:5mons
课程价钱:3000
tea1.course.tell_info()
课程名:linux运维
课程周期:5mons
课程价钱:800
标签:对象关联 pre 理解 自己的 对象 fine 定义 自己 使用
原文地址:https://www.cnblogs.com/Dr-wei/p/11847850.html