路飛 5。16答案
阿新 • • 發佈:2020-11-02
https://www.cnblogs.com/alice-bj/p/8561425.html
1.面向物件三大特性,各有什麼用處,說說你的理解。
繼承:解決程式碼重用問題
多型:多型性,可以在不考慮物件型別的情況下而直接使用物件
封裝:明確的區分內外,控制外部對隱藏屬性的操作行為,隔離複雜度
2.類的屬性和物件的屬性有什麼區別?
類的屬性:資料屬性和函式屬性,資料屬性是所有物件共有的,函式屬性是繫結給物件使用的
物件的屬性:物件是類的例項化
3.面向過程程式設計與面向物件程式設計的區別與應用場景?
面向過程:複雜的問題流程化,簡單化 應用場景:不在需要擴充套件了,監控指令碼,自動部署指令碼,軟體解壓安裝
面向物件:特徵與技能的結合體 一切皆物件 應用場景:使用者需求經常變化,網際網路應用,遊戲,企業內部應用
4.類和物件在記憶體中是如何儲存的。
類和物件的屬性:以字典的形式儲存的。
5.什麼是繫結到物件的方法、繫結到類的方法、解除繫結的函式、如何定義,如何呼叫,給誰用?有什麼特性
繫結到物件的方法:就應該由物件來呼叫,def tell_info(self):... obj.tell_info()
繫結到類的方法:就應該由類來呼叫,@classmethod def from_conf(cls):... class.from_conf()
非繫結方法:不與類或物件繫結,誰都可以呼叫,@staticmethod def create_id():... obj.create_if()/class.create_id()
6.使用例項進行 獲取、設定、刪除 資料, 分別會觸發類的什麼私有方法
1 # class A(object):
2 # def __setitem__(self, key, value):
3 # self.__dict__[key] = value
4 #
5 # def __getitem__(self, item):
6 # # return self.__dict__[item]
7 # return self.__dict__.get(item)
8 #
9 # def __delitem__(self, key):
10 # del self.__dict__[key]
11 #
12 # a = A()
13 # a["key"] = "val"
14 # print(a.__dict__)
15 # # a = a["key"]
16 # # print(a)
17 # del a["key"]
18 # print(a.__dict__)
7.python中經典類和新式類的區別
經典類:py2 沒有繼承object的類,以及它的子類都稱之為經典類 -->深度優先
新式類:py3 繼承object的類,以及它的子類都稱之為新式類 -->廣度優先
8.如下示例, 請用面向物件的形式優化以下程式碼
1 # 1、在沒有學習類這個概念時,資料與功能是分離的
2 # def exc1(host,port,db,charset):
3 # conn=connect(host,port,db,charset)
4 # conn.execute(sql)
5 # return xxx
6 # def exc2(host,port,db,charset,proc_name)
7 # conn=connect(host,port,db,charset)
8 # conn.call_proc(sql)
9 # return xxx
10 # # 每次呼叫都需要重複傳入一堆引數
11 # exc1('127.0.0.1',3306,'db1','utf8','select * from tb1;')
12 # exc2('127.0.0.1',3306,'db1','utf8','儲存過程的名字')
13
14 # class exec:
15 # def __init__(self,proc_name):
16 # self.host='127.0.0.1'
17 # self.port=3306
18 # self.db='db1'
19 # self.charset='utf-8'
20 # self.proc_name=proc_name
21 #
22 # ex=exec('儲存名稱')
23 # ex2=exec('儲存名稱2')
24 # print(ex.__dict__)
25 # print(ex2.__dict__)
9.示例1, 現有如下程式碼, 會輸出什麼:
1 class People(object):
2 __name = "luffy"
3 __age = 18
4
5
6 p1 = People()
7 # print(p1.__name, p1.__age)
8 '''
9 AttributeError: 'People' object has no attribute '__name'
10 '''
11 # print(People.__dict__)
12 # print(p1._People__name,p1._People__age)
13 '''
14 {'__module__': '__main__', '_People__name': 'luffy', '_People__age': 18, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
15 luffy 18
16 '''
10.示例2, 現有如下程式碼, 會輸出什麼:
1 class People(object):
2
3 def __init__(self,name):
4 print("__init__")
5
6 def __new__(cls, *args, **kwargs):
7 print("__new__")
8 print(args,kwargs)
9 return object.__new__(cls)
10
11 # People('alice')
12 '''
13 __new__
14 __init__
15 '''
11.請簡單解釋Python中 staticmethod(靜態方法)和 classmethod(類方法), 並分別補充程式碼執行下列方法。
靜態方法:非繫結方法,類和物件都可呼叫
類方法:繫結給類的方法,類呼叫
1 class A(object):
2 def __init__(self,name):
3 self.name=name
4
5 def foo(self, x):
6 print("executing foo(%s, %s)" % (self,x))
7
8 @classmethod
9 def class_foo(cls, x):
10 print("executing class_foo(%s, %s)" % (cls,x))
11
12 @staticmethod
13 def static_foo(x):
14 print("executing static_foo(%s)" % (x))
15
16 # a = A('alice')
17 # a.foo('alice')
18 # A.class_foo('alice')
19 # a.static_foo('alice')
20 # A.static_foo('alice')
21 '''
22 executing foo(<__main__.A object at 0x000002A5FED12AC8>, alice)
23 executing class_foo(<class '__main__.A'>, alice)
24 executing static_foo(alice)
25 executing static_foo(alice)
26 '''
12.請執行以下程式碼,解釋錯誤原因,並修正錯誤。
錯誤原因:@property 可將函式屬性轉化為資料屬性
1 class Dog(object):
2
3 def __init__(self,name):
4 self.name = name
5
6 @property
7 def eat(self):
8 print(" %s is eating" %self.name)
9
10 d = Dog("ChenRonghua")
11 # d.eat()
12 # d.eat
13.下面這段程式碼的輸出結果將是什麼?請解釋。
1 class Parent(object):
2 x = 1
3
4 class Child1(Parent):
5 pass
6
7 class Child2(Parent):
8 pass
9
10 # print(Parent.x, Child1.x, Child2.x)
11 # Child1.x = 2
12 # print(Parent.x, Child1.x, Child2.x)
13 # Parent.x = 3
14 # print(Parent.x, Child1.x, Child2.x)
15
16 # 1 1 1 繼承自父類的類屬性x,所以都一樣,指向同一塊記憶體地址
17 # 1 2 1 更改Child1,Child1的x指向了新的記憶體地址
18 # 3 2 3 更改Parent,Parent的x指向了新的記憶體地址
14.多重繼承的執行順序,請解答以下輸出結果是什麼?並解釋。
super()表示的是 子類的mro()列表中的下一個
print(G.mro())
[<class '__main__.G'>, <class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
print(F.mro())
[<class '__main__.F'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>]
1 class A(object):
2 def __init__(self):
3 print('A')
4 super(A, self).__init__()
5
6 class B(object):
7 def __init__(self):
8 print('B')
9 super(B, self).__init__()
10
11 class C(A):
12 def __init__(self):
13 print('C')
14 super(C, self).__init__()
15
16 class D(A):
17 def __init__(self):
18 print('D')
19 super(D, self).__init__()
20
21 class E(B, C):
22 def __init__(self):
23 print('E')
24 super(E, self).__init__()
25
26 class F(C, B, D):
27 def __init__(self):
28 print('F')
29 super(F, self).__init__()
30
31 class G(D, B):
32 def __init__(self):
33 print('G')
34 super(G, self).__init__()
35
36 # if __name__ == '__main__':
37 # g = G()
38 # f = F()
39 # print(G.mro())
40 # print(F.mro())
41
42 # G
43 # D
44 # A
45 # B
46 #
47 # F
48 # C
49 # B
50 # D
51 # A
52 # [<class '__main__.G'>, <class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
53 # [<class '__main__.F'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>]
15.請編寫一段符合多型特性的程式碼.
1 class Animal:
2 def __init__(self, name):
3 self.name = name
4
5
6 class People(Animal):
7 def talk(self):
8 print('%s is talking' % self.name)
9
10
11 class Dog(Animal):
12 def talk(self):
13 print('%s is talking' % self.name)
14
15
16 def func(animal):
17 animal.talk()
18
19 # p=People('alice')
20 # d=Dog('wang')
21 # func(p)
22 # func(d)
23 '''
24 alice is talking
25 wang is talking
26 '''
16.很多同學都是學會了面向物件的語法,卻依然寫不出面向物件的程式,原因是什麼呢?原因就是因為你還沒掌握一門面向物件設計利器,
即領域建模,請解釋下什麼是領域建模,以及如何通過其設計面向物件的程式?
http://www.cnblogs.com/alex3714/articles/5188179.html 此blog最後面有詳解
領域模型,顧名思義,就是需求所涉及的領域的一個建模,更通俗的講法是業務模型。
定義:
需求到面向物件的橋樑
作用:
1.發掘重要的業務領域概念
2.建立業務領域概念之間的關係
方法:
從用例中找名詞
領域建模的三字經方法:找名詞、加屬性、連關係。
參考:http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label15
http://www.cnblogs.com/linhaifeng/articles/7341318.html
17.請寫一個小遊戲,人狗大站,2個角色,人和狗,遊戲開始後,生成2個人,3條狗,互相混戰,人被狗咬了會掉血,狗被人打了也掉血,
狗和人的攻擊力,具備的功能都不一樣。注意,請按題14領域建模的方式來設計類。
1 class Animal:
2 def __init__(self, name,life_value,aggressivity):
3 self.name = name
4 self.life_value = life_value
5 self.aggressivity = aggressivity
6
7 def attack(self,enemy):
8 enemy.life_value -= self.aggressivity
9
10
11 class People(Animal):
12 camp='home'
13 def attack(self,enemy):
14 super().attack(enemy)
15 print('from people')
16
17 class Dog(Animal):
18 camp='wo'
19 def attack(self,enemy):
20 super().attack(enemy)
21 print('from dog')
22
23 p1=People('alice',80,30)
24 p1=People('alex',80,30)
25 d1=Dog('w1',90,50)
26 d2=Dog('w2',90,50)
27 d3=Dog('w3',90,50)
28
29 # print(p1.life_value)
30 # d1.attack(p1)
31 # print(p1.life_value)
32
33 # print(d1.life_value)
34 # p1.attack(d1)
35 # print(d1.life_value)
18.編寫程式, 在元類中控制把自定義類的資料屬性都變成大寫.
19.編寫程式, 在元類中控制自定義的類無需init方法.
1 class Mymeta(type):
2 def __new__(cls,class_name,class_bases,class_dic):
3 update_dic = {}
4 for i in class_dic:
5 if not callable(class_dic[i]) and not i.startswith('__'):
6 update_dic[i.upper()]=class_dic[i]
7 else:
8 update_dic[i]=class_dic[i]
9 return type.__new__(cls,class_name,class_bases,update_dic)
10
11 def __call__(self, *args, **kwargs):
12 obj=object.__new__(self)
13 if args:
14 raise TypeError('must be keyword argument')
15 for i in kwargs:
16 obj.__dict__[i]=kwargs[i]
17 return obj
18
19 class Chinese(metaclass=Mymeta):
20 country='china'
21 tag='legend of the dragon'
22
23 def talk(self):
24 print('%s is talking'%self.name)
25
26 # print(Chinese.__dict__)
27 # ch=Chinese(name='alice',age=18)
28 # ch.talk()
29 # print(ch.__dict__)
20.編寫程式, 編寫一個學生類, 要求有一個計數器的屬性, 統計總共例項化了多少個學生.
1 class Student:
2 __count = 0
3 def __init__(self, name, age):
4 self.name = name
5 self.age = age
6 Student.__count += 1
7
8 @property
9 def talk(self):
10 print('%s is talking' % self.name)
11
12 @staticmethod
13 def tell_count():
14 print('總共例項化了 %s 人' % Student.__count)
15
16 # s1 = Student('alice', 18)
17 # s2 = Student('alex', 20)
18 # s3 = Student('egon', 28)
19 # Student.tell_count()
20 # s1.tell_count()
21 # s1.talk
22 # s2.talk
21.編寫程式, A 繼承了 B, 倆個類都實現了 handle 方法, 在 A 中的 handle 方法中呼叫 B 的 handle 方法
1 class B:
2 def handle(self):
3 print('from B handle')
4
5 class A(B):
6 def handle(self):
7 super().handle()
8 # print('from A handle')
9 # a=A()
10 # a.handle()
22.編寫程式, 如下有三點要求:
1.自定義使用者資訊資料結構, 寫入檔案, 然後讀取出內容, 利用json模組進行資料的序列化和反序列化
e.g
{
"egon":{"password":"123",'status':False,'timeout':0},
"alex":{"password":"456",'status':False,'timeout':0},
}
2.定義使用者類,定義方法db,例如 執行obj.db可以拿到使用者資料結構
3.在該類中實現登入、退出方法, 登入成功將狀態(status)修改為True, 退出將狀態修改為False(退出要判斷是否處於登入狀態).
密碼輸入錯誤三次將設定鎖定時間(下次登入如果和當前時間比較大於10秒即不允許登入)
1 import json
2 import time
3 class User:
4 def __init__(self, name, password):
5 self.name = name
6 self.password = password
7 self.status = False
8 self.timeout = 0
9
10 @property
11 def db(self):
12 with open(self.name+'.txt', 'r', encoding='utf-8') as f:
13 data = json.load(f)
14 return data
15
16 def save(self):
17 obj={}
18 obj[self.name]={'password':self.password,'status':self.status,'timeout':self.timeout}
19 with open(self.name+'.txt', 'w', encoding='utf-8') as f:
20 json.dump(obj,f)
21
22 def login(self):
23 with open(self.name+'.txt', 'r+', encoding='utf-8') as f:
24 data = json.load(f)
25 count = 0
26 while count < 3:
27 password = input('password>>:').strip()
28 if password != data[self.name]['password']:
29 count += 1
30 continue
31 else:
32 if data[self.name]['timeout'] != 0:
33 if time.time() - data[self.name]['timeout'] > 10:
34 print('不允許登入了!超時')
35 break
36 else:
37 data[self.name]['status'] = True
38 f.seek(0)
39 f.truncate()
40 json.dump(data, f)
41 print('----welcome----')
42 break
43 else:
44 data[self.name]['status'] = True
45 f.seek(0)
46 f.truncate()
47 json.dump(data, f)
48 print('----welcome----')
49 break
50
51 else:
52 data[self.name]['timeout']=time.time()
53 f.seek(0)
54 f.truncate()
55 json.dump(data,f)
56
57
58 def quit(self):
59 with open(self.name+'.txt', 'r+', encoding='utf-8') as f:
60 data = json.load(f)
61 if data[self.name]['status'] == True:
62 data[self.name]['status'] = False
63 f.seek(0)
64 f.truncate()
65 json.dump(data, f)
66 else:
67 print('您是退出狀態!')
68
69
70 # alex=User('alex','123')
71 # egon=User('egon','456')
72 # # alex.save()