您的位置:首页 > 编程语言 > Python开发

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

2017-07-24 12:35 337 查看
一、区别

面向过程:在实现的时候,每个过程都需要一个函数

面向对象:

二、面向对象和类

类的组成:以狗为例

(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: