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類似
