1. 程式人生 > >python:面向過程和麵向物件程式設計思想

python:面向過程和麵向物件程式設計思想

一、區別
面向過程:在實現的時候,每個過程都需要一個函式
面向物件:
二、面向物件和類
類的組成:以狗為例
(1)類名:(狗)
(2)類的屬性:一組資料(狗的毛色,重量等)
(3)類的方法:(狗的功能)
三、全域性變數
實際上就是使用self初始化,然後就可以在類的方法裡面直接呼叫該變數

class Cat:
    def __init__(self,new_name,new_age):
        self.name=new_name
        self.age=new_age
    def __str__(self):
        return '%s age is %d.'
%(self.name,self.age) def eat(self): print 'eating...' def drink(self): print 'drink...' def introduce(self): print ('%s age is:%d.'%(self.name,self.age)) if __name__=='__main__': tom=Cat('tom',40) tom.introduce() bluecat=Cat('Bluecat',20) bluecat.introduce() print
tom print bluecat