class Person: def play(self,tool): # 經過參數的傳遞,把另外一個類傳遞進來 tool.run() print('玩遊戲上天啊') class Computer: def run(self): print('電腦開機') class Phone: def run(self): print('手機解鎖') c = Computer() p = Phone() s = Person() s.play(c) s.play(p) # 電腦開機 # 玩遊戲上天啊 # 手機解鎖 # 玩遊戲上天啊
# 植物大戰殭屍 class Plant: def __init__(self,name,blood,assault): self.name = name self.blood = blood self.assault = assault def attact(self,ghost): print('植物攻擊殭屍') ghost.blood -= self.assault print(f"殭屍掉血{self.assault}還剩下{ghost.blood}") class Ghost: def __init__(self,name,blood,assault): self.name = name self.blood = blood self.assault = assault def attact(self,plant): print('殭屍吃植物') plant.blood -= self.assault print(f"植物掉血{self.assault}還剩下{plant.blood}") p = Plant('妖嬈仙人掌',100,300) g = Ghost('鐵桶殭屍',1000,20) p.attact(g) g.attact(p) p.attact(g) g.attact(p) p.attact(g)
class Boy: def __init__(self,name, girlFriend=None): # 在初始化的時候能夠給一個對象的屬性設置成另外一個類的對象 self.girlFriend = girlFriend # 一個男孩有一個女友 def eat(self): if self.girlFriend: print(f"帶着他的女友{self.girlFriend.name}去吃飯") else: print("單身狗,吃什麼吃,快去學習") def movie(self): if self.girlFriend: print(f"帶着他的女友{self.girlFriend.name}去看電影") else: print("單身狗,看什麼看,快去學習") class Girl: def __init__(self,name): self.name = name b =Boy("張磊") g = Girl("孫冪") b.eat() b.girlFriend =g b.eat()
二、一對多關係 app
class School: def __init__(self,name): self.name = name self.teach_list = [] def employ(self,teacher): self.teach_list.append(teacher) def goclass(self): for i in self.teach_list: i.work() class Teacher: def __init__(self,name): self.name = name def work(self): print(f'{self.name}老師去上課了') s = School('哈佛') t1 = Teacher('zhangmeng') t2 = Teacher('sunxinag') t3 = Teacher('lina') t4= Teacher('zhenjia') t5= Teacher('lixiao') t6 = Teacher('zhouying') t7 = Teacher('huyang') s.employ(t1) s.employ(t2) s.employ(t3) s.employ(t4) s.employ(t5) s.employ(t6) s.employ(t7) s.goclass()
# 去掉可哈希 class Foo: __hash__ = None # 當前類的對象不可哈希 print(hash(Foo)) # 可哈希 print(hash(Foo())) # TypeError: unhashable type: 'Foo'
class Foo(): def eat(self,food): print("我愛吃魚和",food) class Bar: def eat(self,food): print("我愛吃肉和",food) dic = {Foo:"雞蛋",Bar:"香腸"} for k,v in dic.items(): k().eat(v) # 我愛吃魚和 雞蛋 # 我愛吃肉和 香腸 # 類名至關於變量名
繼承 ide
class Base: def __init__(self,num): self.num = num def func1(self): print(self.num) self.func2() def func2(self): print(111,self.num) class Foo(Base): def func2(self): print(222,self.num) lst = [Base(1), Base(2),Foo(3)] for obj in lst: obj.func1() # 1 # 111 1 # 2 # 111 2 # 3 # 222 3
特殊成員 學習
__init__() #建立對象的時候初始化操做,構造器 __del__() # 析構器,當一個實例被銷燬的時候調用的方法 __call__() # 對象() __getitem__() # 對象[key] __setitem__() # 對象[key] = value __new__() # 建立對象的時候,開闢內存 __enter__() # with對象 as 變量 __exit__() # 結束with的時候 __hash__() # 可哈希 hash() __hash__ = None 幹掉可哈希 __str__ 打印對象的時候會自動執行 __delitem__() del對象[key]時執行 __add__() 對象+對象 __doc__ 查看類的描述信息 __module__ 表示當前操做的對象所在模塊 __class__ 當前操做對象所處的類 __iter__() 定義迭代器 __contains__(self,item) 定義當使用成員測試運算符(in 活 not in)時的行爲
class Foo: def __init__(self): # 初始化操做 print("我是init, 我是老二") print("初始化操做. 在建立對象的時候自動調用這個方法") def __new__(cls, *args, **kwargs): # 建立, 它是真正的構造方法, 能夠開闢內存 print("我是new. 我是老大") return object.__new__(cls) # 爲了 對象() def __call__(self, *args, **kwargs): print("我是對象()") # 對象[] def __getitem__(self, item): print("item=",item) print("你執行了__getitem__") return "哈哈" # 對象[key] = value def __setitem__(self, key, value): print("key, ", key) print("value, ", value) # del lst[1] def __delitem__(self, key): print("key=", key) # with 對象: def __enter__(self): print("我是enter") # with 對象: 代碼執行完畢. 最後執行這裏 def __exit__(self, exc_type, exc_val, exc_tb): print("我叫exit") def __len__(self): print("個人天哪") return 3 f = Foo() # 自動執行__init__() f() # 調用-> __call__() print(callable(f)) # 對象() print(f["李嘉誠"]) # 自動調用__getitem__() f['jay'] = "林俊杰" del f['哈哈'] with f: print("我是哈哈哈哈") with open() : lst = ["孫藝珍", "李金珠", "井柏然"] lst[2] # =>自動的調用__getitem__() def func(): pass func = 3 print(callable(func)) # 判斷xxx是不是可調用的 f.__init__() # 第一次這麼寫. 之後別這麼寫 lst = [1,2,3,4] it = iter(lst) print(it.__next__()) print(next(it)) # __next__()