簡(jiǎn)介
- 面向過程:根據(jù)操作數(shù)據(jù)的函數(shù)或語句塊來設(shè)計(jì)程序。
- 面向?qū)ο螅喊褦?shù)據(jù)和功能結(jié)合起來,用成為對(duì)象的東西包裹起來組織程序。
- 類和對(duì)象:類創(chuàng)建一個(gè)新類型,對(duì)象是這個(gè)類的實(shí)例。a是int類型的變量,則存儲(chǔ)整數(shù)的變量是int類的對(duì)象(實(shí)例)。
- 域:屬于一個(gè)對(duì)象或類的變量被稱為域。
- 類的方法:對(duì)象可以使用屬于類的函數(shù)來具有功能,這樣的函數(shù)被稱為類的方法。
域和方法可以合稱為類的屬性。 - self:例如一個(gè)類名為MyClass和對(duì)應(yīng)的一個(gè)實(shí)例MyObject。當(dāng)調(diào)用這個(gè)對(duì)象的方法MyObject.method(arg1,arg2)時(shí),會(huì)由python自動(dòng)轉(zhuǎn)化為MyClass.method(self,arg1,arg2)。
類的函數(shù)→類的方法→對(duì)象的方法→調(diào)用
類
創(chuàng)建類
- class+類名
class Myclass:
pass
o = Myclass()
print(o)
輸出:<__main__.Myclass object at 0x000001B6799C2F28>。即已經(jīng)在__main__模塊中創(chuàng)建了一個(gè)Myxlass類的實(shí)例,存儲(chǔ)對(duì)象的地址也顯示出來。python在任何空位存儲(chǔ)對(duì)象。
類/對(duì)象可以擁有像函數(shù)一樣的方法,這些方法與函數(shù)的區(qū)別只是一個(gè)額外的self變量
對(duì)象
創(chuàng)建對(duì)象
- (對(duì)象名 = )類名+()
class Female:
def feature(self):
print("beautful")
class male:
def feature(self):
print("strong")
Zoe = Female()
Zoe.feature() #same as Female().feature()
Tom = male()
Tom.feature()
輸出:beautiful strong
方法
__init__方法在類的一個(gè)對(duì)象被創(chuàng)建后,馬上運(yùn)行。這個(gè)方法可以用來對(duì)創(chuàng)建的對(duì)象做有用的初始化。
class Female:
def __init__(self,age):
self.age = age
def feature(self):
print("beautful")
class male:
def __init__(self,age):
self.age = age
def feature(self):
print("strong")
#兩種方法
Female(23).feature()
Tom = male(23)
Tom.feature()
輸出:beautiful strong
相當(dāng)于類別的初始屬性
類與對(duì)象的變量
類的變量(域)共享
對(duì)象的變量(域)不共享
#原書實(shí)例
class Person:
"""Represents a person."""
population = 0
def __init__(self, name):
'''Initializes the person's data.'''
self.name = name
print ('Initializing %s'% self.name)
# When this person is created, he/she
# adds to the population
Person.population += 1
def __del__(self):
'''I am dying.'''
print ('%s says bye.'% self.name)
Person.population -= 1
if Person.population == 0:
print( 'I am the last one.')
else:
print('There are still %d people left.'% Person.population)
def sayHi(self):
'''Greeting by the person.
Really, that's all it does.'''
print ('Hi, my name is %s.'% self.name)
def howMany(self):
'''Prints the current population.'''
if Person.population == 1:
print ('I am the only person here.')
else:
print ('We have %d persons here.'% Person.population)
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
swaroop.sayHi()
swaroop.howMany()
輸出:
Initializing Swaroop
Hi, my name is Swaroop.
I am the only person here.
Initializing Abdul Kalam
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Swaroop says bye.
There are still 1 people left.
Abdul Kalam says bye.
I am the last one.
注:
1.此代碼中,Person.population是類的變量;self.name是對(duì)象的變量。其實(shí)很簡(jiǎn)單,看·的前面是什么就知道是類的變量還是對(duì)象的變量。
2.當(dāng)對(duì)象不再被使用時(shí),del方法運(yùn)行,但是很難保證這個(gè)方法究竟在 什么時(shí)候 運(yùn)行。
類的變量→全局變量 & 對(duì)象的變量→局部變量
繼承
基本類/超類,導(dǎo)出類/子類
父類改變影響子類
子類改變不影響其他類
class people:
def __init__(self, name, age):
self.name = name
self.age = age
print('initializing people %s' % self.name)
def details(self):
print('name %s,age %s'%(self.name, self.age))
class adult(people):
def __init__(self, name, age, height):
people.__init__(self, name, age)
self.height = height
print('initializing adult %s' % self.name)
def details(self):
people.details(self)
print('height %d' % self.height)
class child (people):
def __init__(self, name, age, gender):
people.__init__(self, name, age)
self.gender = gender
print('initializing child %s' % self.name)
def details(self):
people.details(self)
print('gender %s' % self.gender)
a = adult('tony', '23', 172)
b = child('alex', '12', 'girl')
a.details()
b.details()
輸出:
initializing people tony
initializing adult tony
initializing people alex
initializing child alex
name tony,age 23
height 172
name alex,age 12
gender girl
#如何不換行輸出
- 為了使用繼承,我們把基本類的名稱作為一個(gè)元組跟在定義類時(shí)的類名稱之后
- 如果在繼承元組中列了一個(gè)以上的類,那么它就被稱作多重繼承
- Python總是首先查找對(duì)應(yīng)類型的方法,在這個(gè)例子中就是如此。如果它不能在導(dǎo) 出類中找到對(duì)應(yīng)的方法,它才開始到基本類中逐個(gè)查找?;绢愂窃陬惗x的時(shí)候,在元組之 中指明的。
附錄
力薦:python簡(jiǎn)明中文教程
本文是此書第十一章的總結(jié)