Python的面向對象編程

面向對象編程心得:

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()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章