面向對象編程心得:
1、面向對象編程的思想是把對象作爲基本單元,一個對象包含數據和數據的操作方法
2、編程時,要先定義類,並初始化實例,最後執行對象的操作方法就可以了
例如,要打印學生的成績單,可以先定義Stuent類,Stuent類包含兩個屬性,即name和score兩個屬性,再定義個操作方法,即print_score函數,即完成了編程。
class Stuent (Object)
def __init__(self,name,score):
self.name = name
self.score = score
def print_score(self):
print "%s:%s" %(self.name,self.score)
a = Stuent("Jack",87)
b = Stuent("David",98)
a.print_score()
b.print_score()