国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中的類學習筆記

2019-11-25 18:12:35
字體:
來源:轉載
供稿:網友

Python使用中面向對象的語言,支持繼承、多態;

定義一個Person類:

復制代碼 代碼如下:

>>> class Person:
... def sayHello(self):
... print('hello')
...
>>> Person.sayHello(None)
hello
>>> Person().sayHello()
hello
 
可以修改Person的類方法
復制代碼 代碼如下:

>>> def hack_sayHello(obj):
... print('...hello')
...
>>>
>>> Person.sayHello = hack_sayHello
>>> Person.sayHello(None)
...hello
>>> Person().sayHello()
...hello
 
>>> sayHello = Person().sayHello
>>> sayHello()
...hello

Person().sayHello也是一個函數,可以賦值給變量,并可以直接調用;
復制代碼 代碼如下:
 
>>> Person.sayHello is Person().sayHello
False
>>> Person.sayHello == Person().sayHello
False
 
Person.sayHello與Person().sayhello并不是同一個對象,直覺上,Person().sayHello關聯(綁定)了一個Person實例,而Person.sayHello是一個類方法;
 
self參數事實上正是方法和函數的區別:方法將它們的第一個參數綁定到所屬的實例上,因此這個參數可以不必提供;
復制代碼 代碼如下:
 
>>> class Person:
... name = 'unkown'
... def sayHello(self):
... print('i/'m ' + name)
...
>>>
>>> Person.sayHello(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
>>> p = Person()
>>> p.name = 'wyj'
>>> p.sayHello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
 
可見,Python在解析變量時,默認從local scope/global scope中查找;
復制代碼 代碼如下:
 
>>> class Person:
... name = 'unkown'
... def sayHello(self):
... print('i/'m ' + self.name)
...
>>>
>>> Person.sayHello(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
AttributeError: 'NoneType' object has no attribute 'name'
>>> p = Person()
>>> p.name = 'wyj'
>>> p.sayHello()
i'm wyj
 
訪問成員都要通過self,假如以包含name屬性的對象調用Person.sayHello(obj),是否可以呢?
復制代碼 代碼如下:

>>> class Cat:
... name = 'huanhuan'
...
>>> Person.sayHello(Cat())
i'm huanhuan
 
可以,Python并不限制必須用相同類的實例對象作為參數調用類方法(貌似Python的類機制類似Javascript);

訪問控制

Python并不直接支持私有方訪問,而是要靠程序員自己把握。
 
不過,可以在屬性名稱前加上雙下劃線而給其私有訪問能力(對外不可見);

復制代碼 代碼如下:

>>> class Person:
... def __private_method(self):
... print('private')
... def test(self):
... self.__private_method()
...
>>> Person().test()
private
>>> Person().__private_method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute '__private_method'
 
實際上,以上下劃線打頭的方法都有一個_ClassName__methodName的方法
復制代碼 代碼如下:

>>> Person._Person__private_method
<function Person.__private_method at 0x7fed431a2d90>
 
調用
復制代碼 代碼如下:

>>> Person._Person__private_method(None)
private

總之,Python并不能阻止從類外進行方法調用;
 
類屬性以及對象屬性

首先,可以為類添加屬性,新對象將得到屬性的一份拷貝

復制代碼 代碼如下:

>>> Person.age = 3
>>> Person().age
3
>>> Person.age = 4
>>> Person().age
4
>>> p = Person()
>>> Person.age = 31
>>> p.age
31

對類屬性的修改,反映到了先前生成的對象的屬性上,這說明類屬性和對象的屬性共享一個值;

復制代碼 代碼如下:

>>> p.age = 34
>>> p.age
34
>>> Person.age
31
>>> Person.age = 99
>>> p.age
34

而一旦對對象的屬性的修改,對象屬性就擁有了自己的值,并不會反映到類屬性上,而對類屬性的修改,也不再反映到該對象的屬性上;

這種行為與Javascript類似

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福泉市| 广德县| 吉首市| 承德市| 广宗县| 屯留县| 临泽县| 五原县| 扶沟县| 大冶市| 尖扎县| 聊城市| 辽宁省| 图们市| 诸城市| 盐城市| 建湖县| 云梦县| 卢湾区| 张掖市| 吉首市| 大新县| 苗栗县| 平阳县| 龙泉市| 安庆市| 西乌珠穆沁旗| 阿瓦提县| 隆子县| 尼木县| 越西县| 茶陵县| 金湖县| 昂仁县| 余江县| 兰西县| 涞水县| 芒康县| 呈贡县| 房产| 东光县|