python-面向物件:類與類之間的關係和特殊成員
阿新 • • 發佈:2018-12-19
1 # class Person: 2 # def play(self, tools): # 通過引數的傳遞把另外一個類的物件傳遞進來 3 # tools.run() 4 # print("很開心, 我能玩兒遊戲了") 5 # 6 # 7 # class Computer: 8 # def run(self): 9 # print("電腦開機. 可以執行") 10 # 11 # class Phone: 12 # def run(self): 13 # print("手機開機, 可以執行")14 # 15 # 16 # c = Computer() 17 # phone = Phone() 18 # 19 # p = Person() 20 # p.play(phone) 21 22 23 24 # 寫一個植物大戰殭屍 25 # 1. 植物 26 # 打殭屍. 殭屍掉血 27 # 2. 殭屍 28 # 吃植物. 植物掉血 29 30 class Plant: 31 def __init__(self, name, hp, ad): # 200 32 self.name = name 33 self.hp = hp34 self.ad = ad 35 36 def attack(self, js): 37 print("植物攻擊殭屍") 38 js.hp -= self.ad 39 print(f"殭屍掉血{self.ad}, 還剩下{js.hp}") 40 41 class JiangShi: 42 def __init__(self, name, hp, ad): # 1000 800 43 self.name = name 44 self.hp = hp 45 self.ad = ad46 47 def attack(self, zw): 48 print("殭屍咬植物") 49 zw.hp -= self.ad 50 print(f"植物掉血{self.ad}, 還剩{zw.hp}") 51 52 53 # 植物 54 wd = Plant("歪脖子豌豆", 10, 20) 55 # 殭屍 56 js = JiangShi("鐵桶殭屍", 200, 1) 57 wd.attack(js) 58 wd.attack(js) 59 wd.attack(js) 60 wd.attack(js) 61 wd.attack(js) 62 63 js.attack(wd) 64 js.attack(wd) 65 js.attack(wd) 66 js.attack(wd)
# class Boy: # # def __init__(self, name, girlFriend=None): # # 在初始化的時候可以給一個物件的屬性設定成另一個類的物件 # self.girlFriend = girlFriend # 一個男孩有一個女朋友 # # def chi(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.chi() # # # alex給包浪介紹了一個女朋. 孫藝珍 # b.girlFriend = g # b.chi() # # g2 = Girl("梁詠琪") # b.girlFriend = g2 # 換了個女朋友 # b.chi() class School: def __init__(self, name): self.teach_list = [] # 這裡要裝多個老師 self.name = name def zhaopin(self, teach): self.teach_list.append(teach) def shangke(self): for t in self.teach_list: t.work() class Teacher: def __init__(self, name): self.name = name def work(self): print(f"{self.name}在上課") lnh = School("老男孩") t1 = Teacher("武sir") t2 = Teacher("太白") t3 = Teacher("哪吒") t4 = Teacher("女神") t5 = Teacher("日天") t6 = Teacher("寶浪") lnh.zhaopin(t1) lnh.zhaopin(t2) lnh.zhaopin(t3)
# class Base: # def chi(self): # print("我會吃") # # # # 派生類 => 子類 # class Foo(Base): # 這個類繼承了Base類. Foo類是對Base的一個擴充套件 # def he(self): # print("我會喝") # # f = Foo() # f.chi() # f.he() # # # class Cat: # 父類 => 基類 => 超類 # def catch_mouse(self): # print("貓可以抓老鼠") # # class BosiCat(Cat): # 子類 => 派生類 # pass # class Foo: # pass # # # print(hash(Foo)) # 可雜湊 # print(hash(Foo())) # 我們寫好的類和建立的物件預設都是可雜湊的 # 去掉可雜湊 # class Foo: # __hash__ = None # 當前類的物件不可雜湊 # print(hash(Foo)) # 可雜湊 # print(hash(Foo())) # TypeError: unhashable type: 'Foo' # # class Foo: # def chi(self, food): # print("我愛吃魚和", food) # # class Bar: # def chi(self, food): # print("我愛吃肉和", food) # # dic = {Foo: "雞蛋", Bar: "香腸"} # # for k, v in dic.items(): # k().chi(v) # 類名 => 變數名 -> 為了今天作業 # def func(): # pass # # an = func # # an() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # class Foo(Base): # pass # obj = Foo(123) # obj.func1() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # # class Foo(Base): # def func1(self): # print("Foo. func1", self.num) # # obj = Foo(123) # obj.func1() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # self.func2() # def func2(self): # print("Base.func2") # # class Foo(Base): # def func2(self): # print("Foo.func2") # obj = Foo(123) # obj.func1() # 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.func2() # 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()
lnh.zhaopin(t4)
lnh.zhaopin(t5)
lnh.zhaopin(t6)
lnh.shangke()
# # 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__() # 面試之前翻一番 # 寫出15個特殊成員, 並給出具體作用 # class H: # country = "大清" # # print(H.country) # 面向物件程式設計的執行流程 -> # 1. 載入類 -> 給類建立一個名稱空間 -> 主要存放類變數. # 2. 建立物件 -> 先找類. -> 根據類來開闢記憶體 -> 執行類中的__new__() -> 執行__init__() -> 返回物件 class Student: def __init__(self, name, no, gender, cls, age): self.name = name self.no = no self.gender = gender self.cls = cls self.age = age # 這個物件字串的表示. def __str__(self): # 返回該物件的字串表示形式 return f"{self.name}, {self.no}, {self.gender}" def __repr__(self): # 該物件的官方的字串表示形式 return f"{self.name}, {self.no}, {self.gender}" s = Student("董仲浩", "3", "男", "S18", "31") print(s)