python-week6
生活随笔
收集整理的這篇文章主要介紹了
python-week6
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.如下示例, 請用面向對象的形式優化以下代碼
? ?def exc1(host,port,db,charset):conn=connect(host,port,db,charset)conn.execute(sql)return xxxdef exc2(host,port,db,charset,proc_name)conn=connect(host,port,db,charset)conn.call_proc(sql)return xxx
? ?# 每次調用都需要重復傳入一堆參數
? ?exc1('127.0.0.1',3306,'db1','utf8','select * from tb1;')
? ?exc2('127.0.0.1',3306,'db1','utf8','存儲過程的名字')
2.多重繼承的執行順序,請解答以下輸出結果是什么?并解釋。
class A(object):def __init__(self):print('A')super(A, self).__init__()class B(object):def __init__(self):print('B')super(B, self).__init__()class C(A):def __init__(self):print('C')super(C, self).__init__()class D(A):def __init__(self):print('D')super(D, self).__init__()class E(B, C):def __init__(self):print('E')super(E, self).__init__()class F(C, B, D):def __init__(self):print('F')super(F, self).__init__()class G(D, B):def __init__(self):print('G')super(G, self).__init__() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??if __name__ == '__main__':g = G()print('-------')f = F() 輸出結果為: G D A B ------ F C B D A # 初始化方法重寫 且有super 父類 子類方法都會調用 # 子類不重寫 __init__,實例化子類時,會自動調用父類定義的 __init__。 # 如果重寫了__init__ 時,實例化子類,就不會調用父類已經定義的 __init__ # 如果重寫了__init__ 時,要繼承父類的構造方法,可以使用 super 關鍵字# 多繼承問題 繼承順序 # 經典類 : 就是深度優先,公共父類被多次執行。 # 新式類 :廣度優先搜索,公共父類只執行一次eg:
經典類:
class A:def __init__(self):print("Enter A")print("Leave A")class B(A):def __init__(self):print("Enter B")A.__init__(self)print("Leave B")class C(A):def __init__(self):print("Enter C")A.__init__(self)print("Leave C")class D(A):def __init__(self):print("Enter D")A.__init__(self)print("Leave D")class E(B, C, D):def __init__(self):print("Enter E")B.__init__(self)C.__init__(self)D.__init__(self)print("Leave E")E() # ---執行結果---- # Enter E # Enter B # Enter A # Leave A # Leave B # Enter C # Enter A # Leave A # Leave C # Enter D # Enter A # Leave A # Leave D # Leave E新式類:
class A(object):def __init__(self):print("Enter A")print("Leave A")class B(A):def __init__(self):print("Enter B")super(B, self).__init__()print("Leave B")class C(A):def __init__(self):print("Enter C")super(C, self).__init__()print("Leave C")class D(A):def __init__(self):print("Enter D")super(D, self).__init__()print("Leave D")class E(B, C, D):def __init__(self):print("Enter E")super(E, self).__init__()print("Leave E")E() # ---執行結果---- # Enter E # Enter B # Enter C # Enter D # Enter A # Leave A # Leave D # Leave C # Leave B # Leave E3、請寫一個小游戲,人狗大站,2個角色,人和狗,游戲開始后,生成2個人,3條狗,互相混戰,人被狗咬了會掉血,狗被人打了也掉血,狗和人的攻擊力,具備的功能都不一樣。注意,請按題14領域建模的方式來設計類。
""" File: 人狗大戰3.py Author: chde_wang Date: 2020-09-13 12:15:03 Description: """ import randomclass Object(object):def __init__(self, name, blood_volume, attack_ability):self.name = nameself.blood_volume = blood_volumeself.attack_ability = attack_abilityclass Person(Object):def attack(self, dog):dog.blood_volume = dog.blood_volume - self.attack_abilityclass Dog(Object):def attack(self, person):person.blood_volume = person.blood_volume - self.attack_abilitydef person_attack_dog(person_list, dog_list):for i in range(len(person_list)):num = random.randint(0, 1)if num == 0:person_list[i].attack(dog_list[0])print(person_list[i].name + "攻擊了" + dog_list[0].name,dog_list[0].name + "血量為:" + str(dog_list[0].blood_volume))elif num == 1:person_list[i].attack(dog_list[1])print(person_list[i].name + "攻擊了" + dog_list[1].name,dog_list[1].name + "血量為:" + str(dog_list[1].blood_volume))def dog_attack_person(person_list, dog_list):for i in range(len(dog_list)):num = random.randint(0, 2)if num == 0:dog_list[i].attack(person_list[0])print(dog_list[i].name + "攻擊了" + person_list[0].name,person_list[0].name + "血量為:" + str(person_list[0].blood_volume))elif num == 1:dog_list[i].attack(person_list[1])print(dog_list[i].name + "攻擊了" + person_list[1].name,person_list[1].name + "血量為:" + str(person_list[1].blood_volume))elif num == 2:dog_list[i].attack(person_list[2])print(dog_list[i].name + "攻擊了" + person_list[2].name,person_list[2].name + "血量為:" + str(person_list[2].blood_volume))p1 = Person("小明", 100, 5) p2 = Person("小張", 100, 4) p3 = Person("小白", 100, 6) d1 = Dog("旺財", 100, 7) d2 = Dog("黑寶", 100, 3)person_list = [] person_list.append(p1) person_list.append(p2) person_list.append(p3)dog_list = [] dog_list.append(d1) dog_list.append(d2)person_attack_dog(person_list, dog_list) print('--------------') dog_attack_person(person_list, dog_list)#小明攻擊了黑寶 黑寶血量為:95 #小張攻擊了黑寶 黑寶血量為:91 #小白攻擊了黑寶 黑寶血量為:85 #-------------- #旺財攻擊了小張 小張血量為:93 #黑寶攻擊了小明 小明血量為:97
4、編寫程序, 編寫一個學生類, 要求有一個計數器的屬性, 統計總共實例化了多少個學生.(提示:@classmethod)
?
總結
以上是生活随笔為你收集整理的python-week6的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: github搜索技巧_和逛知乎、刷微博一
- 下一篇: postman-常见问题解决方案记录