class
team
():def
__init__
(self):
self.name = "tampa bay buccaneers"
bucs = team()
print(bucs.name)
'''tampa bay buccaneers
'''
當呼叫team()函式時,會自動呼叫init函式,self是自動傳入到init中的引數,它指向當前操作的物件。
class
team
():def
__init__
(self,name ):
self.name = name
giants = team("new york giants")
也可以給類建立函式,當呼叫函式時,會自動傳入self引數,呼叫時不用顯示的新增self引數。self用來訪問當前物件的屬性和方法。此處的print_name函式就是例項方法,它要顯示呼叫,而不像init函式一樣被自動呼叫。
class
team
():def
__init__
(self, name):
self.name = name
defprint_name
(self):
print(self.name)
import csv
f = open("nfl.csv", 'r') # game data
nfl = list(csv.reader(f))
# the nfl data is loaded into the nfl variable.
class
team
():def
__init__
(self, name):
self.name = name
defprint_name
(self):
print(self.name)
defcount_total_wins
(self):
count = 0
for row in nfl:
if row[2] == self.name:
count = count + 1
return count
broncos = team("denver broncos")
broncos_wins = broncos.count_total_wins()
chiefs = team("kansas city chiefs")
chiefs_wins = chiefs.count_total_wins()
import csv
class
team
():def
__init__
(self, name):
self.name = name
f = open("nfl.csv", 'r')
csvreader = csv.reader(f)
self.nfl = list(csvreader)
defcount_total_wins
(self):
count = 0
for row in self.nfl: # 由於是物件的屬性,必須加上self
if row[2] == self.name:
count = count + 1
return count
jaguars = team("jacksonville jaguars")
jaguars_wins = jaguars.count_total_wins()
import csv
class
team
():def
__init__
(self, name):
self.name = name
f = open("nfl.csv", 'r')
csvreader = csv.reader(f)
self.nfl = list(csvreader)
defcount_total_wins
(self):
count = 0
for row in self.nfl:
if row[2] == self.name:
count = count + 1
return count
defcount_wins_in_year
(self, year):
count = 0
for row in self.nfl:
if row[2] == self.name and row[0] == year:
count += 1
return count
niners = team("san francisco 49ers")
niners_wins_2013 = niners.count_wins_in_year("2013")
c 物件導向程式設計 物件導向
什麼是物件導向 是對現實世界理解和抽象的方法。物件導向程式設計的特點 易維護,易擴充套件,靈活性好,重用 類 對事物的抽象定義,即事物具有的共同特徵和行為。物件 即對類進行例項 類是抽象的,物件是具體的 類的定義 語法 訪問修飾符 class 類名類的成員 包括字段,屬性,方法,常量,事件和索引器等...
物件導向程式設計
物件導向程式設計 抽象 封裝 繼承 多型 單一職責原則 就乙個類而言,應該僅有乙個引起它變化的原因。開放封閉原則 軟體實體應該可以擴充套件,但是不可修改。所有的設計模式都是對不同的可變性進行封裝,從而使系統在不同的角度上達到開發封閉原則。依賴倒轉原則 a 高層不應該依賴低層,兩個都應該依賴抽象。b ...
物件導向程式設計
物件導向程式設計 object oriented programming,簡稱oop,是一種程式設計思想。oop把物件作為程式的基本單元,乙個物件包含了資料和運算元據的函式。面向過程的程式設計把電腦程式視為一系列的命令集合,即一組函式的順序執行。為了簡化程式設計,面向過程把函式繼續切分為子函式,即把...