1.类与对象的概念

对象:特征与技能的集合体
类:一系列对象相似的特征与技能的集合体
即动物类:
特征:鼻子,眼睛,耳朵,嘴巴
技能:吃,跑
在现实世界中,肯定是现有对象,然后人类根据对象的一些共有特征,对其进行分类。
在编程中,需要先定义类,然后实例化产生对象

2.定义类

"现实世界中,先有对象,后产生类"对象1:张三 特征: 学校='my_school' 姓名=张三 性别=男 年龄=18 技能: 学习 吃饭 睡觉对象2:李四 特征: 学校='my_school' 姓名=李四 性别=女 年龄=38 技能: 学习 吃饭 睡觉现实中的学生类 相似的特征: 学校='my_school' 相似的技能: 学习 吃饭 睡觉

"程序中,先有类,后使用类产生对象"#在Python中程序中的类用class关键字定义,而在程序中特征用变量标识,技能用函数标识,因而类中最常见的无非是:变量和函数的定义#定义类class Student: school='my_school' def learn(self): print('is learning') def eat(self): print('is eating') def sleep(self): print('is sleeping')#后产生对象s1 = Student()s2 = Student()print(s1)print(s2)E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test2/ww/ee.py<__main__.Student object at 0x000001AF8F10EBA8><__main__.Student object at 0x000001AF8F10EB70>Process finished with exit code 0

"注意:1.类中可以有任意python代码,这些代码是在类定义阶段就会执行,因而会产生新的名称空间,用来存放类的变量名与函数名,可以通过Student.__dict__查看2.类中定义的名字,都是类的属性,可以通过Student.来访问属性3.对于经典类,我们可通过__dict__产生的字典操作类名称空间的名字,但新式类有限"#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: vitaclass Student: school = 'my_school' print(school) # 验证类中的代码,在定义阶段就会执行,因而会产生新的名称空间 def learn(self): print('is learning') def eat(self): print('is eating') def sleep(self): print('is sleeping')print("Student.__dict__:",Student.__dict__) # 验证可通过Student.__dict__查看类名称空间中的变量与函数名print("Student.school:",Student.school) #验证可通过.访问类中的属性print("Student.__dict__['school']",Student.__dict__["school"])# 验证可通过访问字典方式访问类中属性E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.pymy_schoolStudent.__dict__: {'__module__': '__main__', 'school': 'my_school', 'learn': <function Student.learn at 0x000001F9B0CD1C80>, 'eat': <function Student.eat at 0x000001F9B0CD1D90>, 'sleep': <function Student.sleep at 0x000001F9B0CD1D08>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}Student.school: my_schoolStudent.__dict__['school'] my_schoolProcess finished with exit code 03.类的使用3.1类属性的增删改查

#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: vitaclass Student: school = 'my_school' #数据属性 def learn(self): #函数属性 print('is learning') def eat(self): #函数属性 print('is eating') def sleep(self): print('is sleeping')#查看类的名称空间print("查看类的名称空间Student.__dict__:",Student.__dict__)print("查看类的名称空间Student.__dict__['school']:",Student.__dict__["school"])#查询属性print("查询Student.school",Student.school)print("查询Student.__dict__['school']:",Student.__dict__["school"])#增加属性Student.country='China'print("增加后的Student.__dict__['country']:",Student.__dict__["country"])#删除属性del Student.countryprint("删除后的Student.__dict__:",Student.__dict__)#修改属性Student.school="new_shcool"print("修改后的Student.__dict__['school']:",Student.__dict__["school"])E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py查看类的名称空间Student.__dict__: {'__module__': '__main__', 'school': 'my_school', 'learn': <function Student.learn at 0x0000021E0D601D90>, 'eat': <function Student.eat at 0x0000021E0D601D08>, 'sleep': <function Student.sleep at 0x0000021E0D60B158>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}查看类的名称空间Student.__dict__['school']: my_school查询Student.school my_school查询Student.__dict__['school']: my_school增加后的Student.__dict__['country']: China删除后的Student.__dict__: {'__module__': '__main__', 'school': 'my_school', 'learn': <function Student.learn at 0x0000021E0D601D90>, 'eat': <function Student.eat at 0x0000021E0D601D08>, 'sleep': <function Student.sleep at 0x0000021E0D60B158>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}修改后的Student.__dict__['school']: new_shcoolProcess finished with exit code 03.2类的实例化

#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: vitaclass Student: school = 'my_school' #数据属性,属于类的s1.school会先到s1.__dict__中找,找不到,去Student类中找 def learn(self): #函数属性 print('is learning') def eat(self): #函数属性 print('is eating') def sleep(self): print('is sleeping')s1 = Student()s2= Student()# 对象的属性是专属于特定对象的属性,不同对象之间互不影响#查看对象的名称空间#注意:这里为空,因为school变量是绑定给Student类的,给所有对象使用,不是单独给某一个对象使用。函数也是绑定给某个对象的,后面会详细介绍print("查看类的名称空间s1.__dict__:",s1.__dict__)print("查看类的名称空间s2.__dict__:",s2.__dict__)#查询属性print("查询s1.school",s1.school)print("查询s2.school",s2.school)#增加属性#增加了属于该对象的属性s1.country='China's2.country='America'print("增加后的s1.__dict__['country']:",s1.__dict__["country"])print("增加后的s1.country:",s1.country)print("增加后的s2.country:",s2.country)#删除属性del s1.countryprint("删除后的s1.__dict__:",s1.__dict__)print("删除后的s2.__dict__:",s2.__dict__)#修改属性#注意:这里并没有真正修改类中的scholl变量,而是为s1对象增加了一个school变量s1.school="new_shcool"print("修改后的s1.school",s1.school)print("修改后的s2.school",s2.school)print("修改后的s1.__dict__:",s1.__dict__)print("修改后的s2.__dict__:",s2.__dict__)# 要想真正修改类中的school变量,需要使用Student.school="new_school",并且修改了,对所有对象起作用Student.school="new_school_class"del s1.school #因为之前为s1对象增加了school变量,所以需要删除掉,否则s1.school会先找s1.__dict__中的变量,找不到再去类中找print("修改Student.school后的s1.school",s1.school)print("修改Student.school后的s2.school",s2.school)print("修改Student.school后的s1.__dict__:",s1.__dict__)print("修改Student.school后的s2.__dict__:",s2.__dict__)E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py查看类的名称空间s1.__dict__: {}查看类的名称空间s2.__dict__: {}查询s1.school my_school查询s2.school my_school增加后的s1.__dict__['country']: China增加后的s1.country: China增加后的s2.country: America删除后的s1.__dict__: {}删除后的s2.__dict__: {'country': 'America'}修改后的s1.school new_shcool修改后的s2.school my_school修改后的s1.__dict__: {'school': 'new_shcool'}修改后的s2.__dict__: {'country': 'America'}修改Student.school后的s1.school new_school_class修改Student.school后的s2.school new_school_class修改Student.school后的s1.__dict__: {}修改Student.school后的s2.__dict__: {'country': 'America'}Process finished with exit code 03.3init方法与对象的使用

"该方法是在对象产生后才会执行,只用来为对象进行初始化操作,可以是任意代码,但不能有返回值"#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: vitaclass Student: school = 'my_school' def __init__(self,name,sex,age):# 属于对象的变量,在s1.__dict__中可查看到 self.name=name self.sex=sex self.age=age def learn(self): #函数属性 print('is learning') def eat(self): #函数属性 print('is eating') def sleep(self): print('is sleeping')#加上__init__方法后,实例化的步骤# 1.产生一个空对象s1# 2.Student.__init__(s1,'vita','女',27)s1=StudentStudent.__init__(s1,'lyly','女',28)s2=Student('vita','女',27)s3=Student('chaochao','男',30)#查print("s1.__dict__",s1.__dict__)print("s2.__dict__",s2.__dict__)print("s3.__dict__",s3.__dict__)print("查询s2.name",s2.name)#等同于s2.__dict__["name"]#改s2.name="vita_new" #等同于s2.__dict__["name"]="vita_new"print("修改后的s2.__dict__",s2.__dict__)print("修改后的s2.name",s2.name)#删除del s2.name #等同于s2.__dict__.pop("course")print("删除后的s2.__dict__",s2.__dict__)#增加s2.class_name="python" #等同于s2.__dict__["class_name"]="python"print("增加后的s2.__dict__",s2.__dict__)#是否对s3对象有所影响print("操作后的s3.__dict__",s3.__dict__)E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.pys1.__dict__ {'__module__': '__main__', 'school': 'my_school', '__init__': <function Student.__init__ at 0x0000018C0ED71C80>, 'learn': <function Student.learn at 0x0000018C0ED71D90>, 'eat': <function Student.eat at 0x0000018C0ED71D08>, 'sleep': <function Student.sleep at 0x0000018C0ED7B158>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None, 'name': 'lyly', 'sex': '女', 'age': 28}s2.__dict__ {'name': 'vita', 'sex': '女', 'age': 27}s3.__dict__ {'name': 'chaochao', 'sex': '男', 'age': 30}查询s2.name vita修改后的s2.__dict__ {'name': 'vita_new', 'sex': '女', 'age': 27}修改后的s2.name vita_new删除后的s2.__dict__ {'sex': '女', 'age': 27}增加后的s2.__dict__ {'sex': '女', 'age': 27, 'class_name': 'python'}操作后的s3.__dict__ {'name': 'chaochao', 'sex': '男', 'age': 30}Process finished with exit code 03.4说明

1.站的角度不同,定义出的类是不同的
2.现实中的类并不完全等于程序中的类,比如现实中公司类,在程序中需要拆分为部门类,业务类
3.有时为了编程需求,程序中可能出现现实中不存在的类,但这在程序中是很普遍的,需要把现实与程序分别开来