目录python
什么是面向对象?编程
面向对象是一种编程思想。函数
面向过程编程code
核心是“过程”二字,过程指的是作事情的步骤,即先作什么,而后作什么,最后作什么。对象
基于该编程思想编写程序,就相似于一条工厂流水线,一种机械式的思惟方式。it
优势io
逻辑清晰,将复杂的问题流程化,进而简单化。面向对象编程
缺点ast
可扩展性差。function
面向对象编程
核心是“对象”二字,对象指的是特征与技能的结合体。
基于该编程思想编写程序,就相似于在创造世界,一种上帝式的思惟方式。
优势
可扩展性高。
缺点
编写程序的复杂程度要远高于面向过程编程思想。
优先使用面向过程仍是面向对象?
实际编程中须要面向过程结合面向对象一块儿使用
什么是对象
特征和技能的结合体。
什么是类
一系列对象相同的特征与技能的结合体。
在现实世界中
对象是一个个具体存在的事物,类是由人类文明的发展抽象总结出来的。
在程序中
必须遵循,先有类,再由对象。
现实世界中,现有对象,后有类。
现实中的猫对象:
根据对象抽象出类:
程序中先定义类,再产生对象。
class 类名: 对象相同的特征 对象相同的技能 # class: 用来定义类的,class后跟的是类名。 # 类名: 类的名字,用来调用创造对象的。 # 注意: 类名必需要遵循驼峰命名法,python在定义变量与函数时不推荐使用,但在定义类时推荐使用。
定义类时发生的事情:
类在定义时,会产生一个类的名称空间
类内部的全部名字,都会存进名称空间
注意:类在定义阶段已经产生好了名称空间,执行python文件时会执行类内部的代码
定义一个类:
class Cat: att = 'cute' def catch(self): print('catch mouse.') def climb(self): print('climb up a tree.')
查看类的名称空间内全部名字
print(Cat.__dict__)
运行结果:
{'__module__': '__main__', 'att': 'cute', 'catch': <function Cat.catch at 0x0000027ED1EB28C8>, 'climb': <function Cat.climb at 0x0000027EE0F3FB70>, '__dict__': <attribute '__dict__' of 'Cat' objects>, '__weakref__': <attribute '__weakref__' of 'Cat' objects>, '__doc__': None} Process finished with exit code 0
查
print(Cat.att)
运行结果:
cute
改
Cat.att='lovely' print(Cat.att)
运行结果:
lovely Process finished with exit code 0
增
Cat.master='tom' print(Cat.master)
运行结果:
tom Process finished with exit code 0
删
Cat.master='tom' print(Cat.master) del Cat.master print(Cat.master)
运行结果:
tom Traceback (most recent call last): File "D:/data/python/Learn/courses/day19/01 课程内容/代码/1 类.py", line 53, in <module> print(Cat.master) AttributeError: type object 'Cat' has no attribute 'master' Process finished with exit code 1
类名 + () 调用类产生对象。
定义对象发生的事情:
产生一个空的对象的名称空间
会自动触发 __init__
把对象自己以及括号内的参数一并传给 __init__
函数
总结:调用类会产生一个对象,调用类的过程叫作类的实例化,产生的对象称之为类的一个实例。
实例化一个对象:
# 定义一个类 class Cat: att = 'cute' def catch(self): print('catch mouse.') def climb(self): print('climb up a tree.') # 实例化对象 cat1 = Cat() cat2 = Cat() # 查看对象属性 print(cat1.att) cat1.catch() cat1.climb() print(cat2.att) cat2.catch() cat2.climb()
运行结果:
cute catch mouse. climb up a tree. cute catch mouse. climb up a tree. Process finished with exit code 0
方法:类定义阶段定制属性,使用 __init__
函数。
# 定义类 class Cat: def __init__(self, att, variety, age, weight): self.att = att self.varieties = variety self.age = age self.weight = weight def catch(self): print('catch mouse.') def climb(self): print('climb up a tree.') # 实例化对象时传特征 cat1 = Cat('cute', 'Orange Cat', 3, 10) cat2 = Cat('lovey', 'Dragen-Li', 2, 6) print(cat1.att, cat1.varieties, cat1.age, cat1.weight) print(cat2.att, cat2.varieties, cat2.age, cat2.weight)
运行结果:
cute Orange Cat 3 10 lovey Dragen-Li 2 6 Process finished with exit code 0
对象与类的查找顺序:
class Cat: def __init__(self, name, att, variety, age, weight): self.name = name self.att = att self.varieties = variety self.age = age self.weight = weight def catch(self): print(f'{self.name}catch mouse.') def climb(self): print('climb up a tree.')
cat1 = Cat('lucky', 'cute', 'Orange Cat', 3, 10) cat2 = Cat('ball', 'lovey', 'Dragen-Li', 2, 6) print(cat1.name) print(cat2.name)
运行结果:
lucky ball Process finished with exit code 0
print(id(cat1.climb)) print(id(cat2.climb)) print(id(Cat.catch)) cat1.catch() cat2.catch()
运行结果:
1503122635976 1503122635976 1501259299696 lucky catch mouse. ball catch mouse. Process finished with exit code 0