新聞中心
類是什么

類是一種組合信息和行為的方式。舉個例子,我們考慮在物理仿真中建造一個飛船。首先要做的就是追蹤飛船的坐標(x, y)。飛船在代碼中的形式如下:
class Rocket(): def __init__(self): # Each rocket has an (x, y) position self.x = 0 self.y = 0
在類中要做的第一件事就是定義 __init__ 函數(shù)。當對象被創(chuàng)建時,__init__ 函數(shù)就會執(zhí)行,為需要的參數(shù)設置初始值。self 以后會介紹,總之,它是一個可以讓你訪問類中變量的語法。
目前為止,Rocket 類存儲了兩部分的信息,但是它什么也做不了。Rocket 類的第一個核心的行為是:移動。代碼如下所示:
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 類存儲了一些信息,并且能做一些事情。但是你還沒有真正建造一艘自己的飛船。接下來就是建造自己的飛船了,代碼如下:
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)建一個變量如 my_rocket 。然后用類名后跟圓括號給這個變量賦值。這樣就創(chuàng)建了一個類的對象。對象是類的實例,它有類中所有變量的拷貝,并且可以做類中所有定義的行為。在上述代碼中,你可以看到變量 my_rocket 是一個來自 __main__ 程序文件中的 Rocket 對象,這個程序文件存儲在內存中的特定位置。
有了類你就可以定義對象并且使用它的方法。實例如下:
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)使用對象名和點符號來訪問對象的變量和方法。因此為了得到 my_rocket 的 y 值,使用 my_rocket.y 。使用 my_rocket.move_up() 來訪問 move_up() 函數(shù)。
一旦類定義好,你就可以創(chuàng)建任意數(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 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)
如果你知道列表推導式,你也可以將代碼改寫成如下形式:
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)
每一個 rocket 在內存中都是獨立的。擁有自己的 x 和 y 。你可以通過移動不同的飛船來證明這點。如下所示:
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)建一個飛船。你或許會將 x 和 y 存儲在一個字典中,即便是定義很少的飛船都需要寫出很多丑陋的,難以維護的代碼。當加入越來越多的特性,代碼會變得越來越難以維護。這時候你就會發(fā)現(xiàn)基于真實世界模型的類是多么的高效。
當前標題:創(chuàng)新互聯(lián)Python教程:初識python中的類
本文路徑:http://m.fisionsoft.com.cn/article/cdgcjci.html


咨詢
建站咨詢
