python:面向过程和面向对象编程思想

来源:互联网 发布:ubuntu软件中心 编辑:程序博客网 时间:2024/06/05 05:20

一、区别
面向过程:在实现的时候,每个过程都需要一个函数
面向对象:
二、面向对象和类
类的组成:以狗为例
(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
原创粉丝点击