新聞中心
類是什么

類是一種組合信息和行為的方式。舉個(gè)例子,我們考慮在物理仿真中建造一個(gè)飛船。首先要做的就是追蹤飛船的坐標(biāo)(x, y)。飛船在代碼中的形式如下:
class Rocket(): def __init__(self): # Each rocket has an (x, y) position self.x = 0 self.y = 0
在類中要做的第一件事就是定義 __init__ 函數(shù)。當(dāng)對象被創(chuàng)建時(shí),__init__ 函數(shù)就會(huì)執(zhí)行,為需要的參數(shù)設(shè)置初始值。self 以后會(huì)介紹,總之,它是一個(gè)可以讓你訪問類中變量的語法。
目前為止,Rocket 類存儲(chǔ)了兩部分的信息,但是它什么也做不了。Rocket 類的第一個(gè)核心的行為是:移動(dòng)。代碼如下所示:
class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1
現(xiàn)在 Rocket 類存儲(chǔ)了一些信息,并且能做一些事情。但是你還沒有真正建造一艘自己的飛船。接下來就是建造自己的飛船了,代碼如下:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
print("Created")
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a Rocket object.
my_rocket = Rocket()
print(my_rocket)為了使用類,創(chuàng)建一個(gè)變量如 my_rocket 。然后用類名后跟圓括號(hào)給這個(gè)變量賦值。這樣就創(chuàng)建了一個(gè)類的對象。對象是類的實(shí)例,它有類中所有變量的拷貝,并且可以做類中所有定義的行為。在上述代碼中,你可以看到變量 my_rocket 是一個(gè)來自 __main__ 程序文件中的 Rocket 對象,這個(gè)程序文件存儲(chǔ)在內(nèi)存中的特定位置。
有了類你就可以定義對象并且使用它的方法。實(shí)例如下:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a Rocket object, and have it start to move up.
my_rocket = Rocket()
print("Rocket altitude:", my_rocket.y)
my_rocket.move_up()
print("Rocket altitude:", my_rocket.y)使用對象名和點(diǎn)符號(hào)來訪問對象的變量和方法。因此為了得到 my_rocket 的 y 值,使用 my_rocket.y 。使用 my_rocket.move_up() 來訪問 move_up() 函數(shù)。
一旦類定義好,你就可以創(chuàng)建任意數(shù)量的對象。每個(gè)對象都有獨(dú)立的變量空間,互不影響。如下所示:
class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [] for x in range(0,5): new_rocket = Rocket() my_rockets.append(new_rocket) # Show that each rocket is a separate object. for rocket in my_rockets: print(rocket)
如果你知道列表推導(dǎo)式,你也可以將代碼改寫成如下形式:
class Rocket(): # Rocket simulates a rocket ship for a game,# Rocket # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [Rocket() for x in range(0,5)] # Show that each rocket is a separate object. for rocket in my_rockets: print(rocket)
每一個(gè) rocket 在內(nèi)存中都是獨(dú)立的。擁有自己的 x 和 y 。你可以通過移動(dòng)不同的飛船來證明這點(diǎn)。如下所示:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a fleet of 5 rockets, and store them in a list.
my_rockets = [Rocket() for x in range(0,5)]
# Move the first rocket up.
my_rockets[0].move_up()
# Show that only the first rocket has moved.
for rocket in my_rockets:
print("Rocket altitude:", rocket.y)現(xiàn)在類的語法對你來說或許不是很明了。但是考慮不用類來創(chuàng)建一個(gè)飛船。你或許會(huì)將 x 和 y 存儲(chǔ)在一個(gè)字典中,即便是定義很少的飛船都需要寫出很多丑陋的,難以維護(hù)的代碼。當(dāng)加入越來越多的特性,代碼會(huì)變得越來越難以維護(hù)。這時(shí)候你就會(huì)發(fā)現(xiàn)基于真實(shí)世界模型的類是多么的高效。
網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:初識(shí)python中的類
分享鏈接:http://www.5511xx.com/article/cdgcjci.html


咨詢
建站咨詢
