class Student(object):
#類的屬性
def __init__(self, name, score):
self.name = name
self.score = score
#類的方法
def print_score(self):
print('%s: %s' % (self.name, self.score))
>>> bart = Student('Bart Simpson', 59)
>>> bart.name
'Bart Simpson'
>>> bart.score
59
>>> bart.print_score()#方法調(diào)用
Bart Simpson: 59
面向?qū)ο蟮脑O計思想是抽象出Class,根據(jù)類(Class)創(chuàng)建實例(Instance)
Student類名通常是大寫開頭的單詞
object表示該類是從哪個類繼承下來的尊浓,沒有合適的繼承類伪窖,就使用object類
通過定義一個特殊的__init__
方法奔脐,在創(chuàng)建實例的時候躯舔,就把name彭雾,score等屬性綁上去
__init__
方法的第一個參數(shù)永遠是self,表示創(chuàng)建的實例本身
有了__init__
方法串稀,在創(chuàng)建實例的時候除抛,就不能傳入空的參數(shù)
和普通的函數(shù)相比,在類中定義的函數(shù)只有一點不同母截,就是第一個參數(shù)永遠是實例變量self到忽,并且,調(diào)用時微酬,不用傳遞該參數(shù)绘趋。
class Student(object):
def __init__(self, name, score):
self.__name = name
self.__score = score
def print_score(self):
print('%s: %s' % (self.__name, self.__score))
def set_score(self, score):
if 0 <= score <= 100:
self.__score = score
else:
raise ValueError('bad score')
>>> bart = Student('Bart Simpson', 98)
>>> bart.__name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute '__name'
>>> bart._Student__name#不建議這么做
'Bart Simpson'
>>> bart.__name = 'New Name' # 設置__name變量!
>>> bart.__name
'New Name'
>>> bart.get_name() # get_name()內(nèi)部返回self.__name
'Bart Simpson'
如果要讓內(nèi)部屬性不被外部訪問颗管,可以把屬性的名稱前加上兩個下劃線__陷遮,在Python中,實例的變量名如果以__開頭垦江,就變成了一個私有變量(private)
如果又要允許外部代碼修改score怎么辦帽馋?可以再給Student類增加set_score方法,在方法中比吭,可以對參數(shù)做檢查绽族,避免傳入無效的參數(shù)
__xxx__是特殊變量,可以直接訪問
__name是私有變量衩藤,不能直接訪問吧慢,但其實Python解釋器對外把__name變量改成了_Student__name
_name是普通變量,意思就是赏表,“雖然我可以被訪問检诗,但是,請把我視為私有變量瓢剿,不要隨意訪問”
繼承
class Animal(object):
def run(self):
print('Animal is running...')
#繼承
class Dog(Animal):
pass
class Cat(Animal):
pass
dog = Dog()
dog.run()
cat = Cat()
cat.run()
Animal is running...
Animal is running...
多態(tài)
class Dog(Animal):
def run(self):
print('Dog is running...')
class Cat(Animal):
def run(self):
print('Cat is running...')
Dog is running...
Cat is running...
def run_twice(animal):
animal.run()
animal.run()
>>> run_twice(Animal())
Animal is running...
Animal is running...
>>> run_twice(Dog())
Dog is running...
Dog is running...
獲取對象信息
type()
>>> type(123)
<class 'int'>
isinstance()
>>> isinstance(h, Husky)
True
dir() 獲得一個對象的所有屬性和方法
>>> dir('ABC')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
配合getattr()逢慌、setattr()以及hasattr(),我們可以直接操作一個對象的狀態(tài)
>>> hasattr(obj, 'x') # 有屬性'x'嗎间狂?
True
>>> obj.x
9
>>> hasattr(obj, 'y') # 有屬性'y'嗎攻泼?
False
>>> setattr(obj, 'y', 19) # 設置一個屬性'y'
>>> hasattr(obj, 'y') # 有屬性'y'嗎?
True
>>> getattr(obj, 'y') # 獲取屬性'y'
19
>>> obj.y # 獲取屬性'y'
19
實例屬性和類屬性
給實例綁定屬性的方法兩種
class Student(object):
def __init__(self, name):
self.name = name
s = Student('Bob')
s.score = 90
給類綁定一個屬性鉴象,類的所有實例都可以訪問到
>>> class Student(object):
... name = 'Student'
...
>>> s = Student() # 創(chuàng)建實例s
>>> print(s.name) # 打印name屬性忙菠,因為實例并沒有name屬性,所以會繼續(xù)查找class的name屬性
Student
>>> print(Student.name) # 打印類的name屬性
Student
>>> s.name = 'Michael' # 給實例綁定name屬性
>>> print(s.name) # 由于實例屬性優(yōu)先級比類屬性高纺弊,因此只搁,它會屏蔽掉類的name屬性
Michael
>>> print(Student.name) # 但是類屬性并未消失,用Student.name仍然可以訪問
Student
>>> del s.name # 如果刪除實例的name屬性
>>> print(s.name) # 再次調(diào)用s.name俭尖,由于實例的name屬性沒有找到氢惋,類的name屬性就顯示出來了
Student
千萬不要把實例屬性和類屬性使用相同的名字洞翩,因為相同名稱的實例屬性將屏蔽掉類屬性,但是當你刪除實例屬性后焰望,再使用相同的名稱骚亿,訪問到的將是類屬性。