day 18 面向对象-类与类之间的关系
# 类与类之间的关系:
# 1.依赖关系:
# 最轻的一种关系
# 在方法中引进另一个类的对象
# 2.关联关系.聚合关系.组合关系
# 类与类之间的关系是比较紧密的
# 3.继承关系.实现关系
# self:当前执行这个方法的对象
# 特殊成员(__init__)
1. 类名() 会⾃自动执⾏行行__init__()
2. 对象() 会⾃自动执⾏行行__call__()
3. 对象[key] 会⾃自动执⾏行行__getitem__()
4. 对象[key] = value 会⾃自动执⾏行行__setitem__()
5. del 对象[key] 会⾃自动执⾏行行 __delitem__()
6. 对象+对象 会⾃自动执⾏行行 __add__()
7. with 对象 as 变量量 会⾃自动执⾏行行__enter__ 和__exit__
8. 打印对象的时候 会⾃自动执⾏行行 __str__
9. ⼲干掉可哈希 __hash__ == None 对象就不可哈希了
# 一对一关系
class Boy:
def __init__(self,name,xingge,girlFriend=None):
self.name=name
self.xingge=xingge
self.girlFriend=girlFriend
def yujian(self,girl):
self.girlFriend=girl
def chi(self):
if self.girlFriend:
print("随便吃%s和%s" % (self.name,self.girlFriend.name))
else:
print("单身狗")
class Girl:
def __init__(self,name,boyFriend):
self.name=name
self.boyFriend=boyFriend
def chi(self):
print("%s在吃饭"% self.name)
girl=Girl("白骨精","李杰")
boy=Boy("金王","开朗")
boy.yujian(girl)
boy.chi()
#一对多
class School:
def __init__(self,name,address,phone):
self.name=name
self.address=address
self.phone=phone
def zhaopin(self,t):
self.__teach_list.append(t)
def display(self):
for el in self.__teach_list:
print(el.name,el.hobby)
class Teacher:
def __init__(self,name,gender,salary,hobby,school):
self.name=name
self.gender=gender
self.salary = salary
self.hobby = hobby
self.school = school
oldboy_bj=School("北京老男孩","沙河","10086")
oldboy_sh=School("北京老男孩,上海分校","上海浦东","10010")
oldboy_sz=School("北京老男孩,深圳分校","南山区","10000")
t1=Teacher("佩奇","男",200,"上课",oldboy_bj)
t2=Teacher("太白","男",150,"开车",oldboy_bj)
t3=Teacher("eggon","男",1588,"钻研技术",oldboy_sh)
t4=Teacher("高新","女",1789,"相夫教子",oldboy_sh)
t5=Teacher("日天","男",666,"春天",oldboy_sz)
oldboy_bj.zhaopin(t1)
oldboy_bj.zhaopin(t2)
oldboy_bj.display()
oldboy_sh.zhaopin(t3)
oldboy_sz.zhaopin(t4)
oldboy_sz.zhaopin(t5)
oldboy_sz.display()