1.面向对象的介绍
2.实现面向对象编程
3.属性查找
4.面向对象的基本语法
5.self的介绍
6.魔法方法
7.运算符相关的魔法方法
1.面向对象的介绍#-*-coding:utf-8-*-"""面向对象:关键点是将程序整合对象是容器,用来存放数据与功能类也是容器,该容器用来存放同类对象共有的数据与功能"""#学生的容器=学生的数据+学生的功能#课程的容器=课程的功能+课程的数据2.实现面向对象编程
注意:类体代码是在定义阶段就会立即执行,会产生类的名称空间
#-*-coding:utf-8-*-#一、先定义类(推荐使用驼峰体)#类是对象类似于数据与功能的集合体#类当中通常包含的是变量和函数,但是类当中其实可以写任意其他代码#注意:类体代码是在定义阶段就会立即执行,会产生类的名称空间classStudents:#1)变量的定义stu_schools=world#2)功能的定义defset_info(stu_obj,x,y,z):stu_obj[stu_name]=xstu_obj[stu_age]=ystu_obj[stu_gender]=z#print(====)#Students.__dict__下面存放的字典#print(Students.__dict__)#print(Students.__dict__[set_info])#类专门的取值的语法#属性访问的语法#1)访问数据属性#print(Students.stu_schools)#本质上是Students.__dict__[set_info]#2)访问函数属性#print(Students.set_info)#添加本质上是操作字典#Students.x=#Students.__dict__[x]=#二、调用类产生对象#对象#stu1_obj=Students()#stu2_obj=Students()#stu3_obj=Students()#底层是在操作字典#代码重复#属性的查找顺序#stu1_obj.stu_name=carter#stu1_obj.__dict__[stu_name]=carter#stu1_obj.stu_age=18#stu1_obj.__dict__[stu_age]=18#stu1_obj.stu_gender=male#stu1_obj.__dict__[stu_gender]=male#print(stu1_obj.__dict__)#stu2_obj.stu_name=carter#stu1_obj.__dict__[stu_name]=carter#stu2_obj.stu_age=18#stu1_obj.__dict__[stu_age]=18#stu2_obj.stu_gender=male#stu1_obj.__dict__[stu_gender]=male#print(stu2_obj.__dict__)#stu3_obj.stu_name=carter#stu1_obj.__dict__[stu_name]=carter#stu3_obj.stu_age=18#stu1_obj.__dict__[stu_age]=18#stu3_obj.stu_gender=male#stu1_obj.__dict__[stu_gender]=male#print(stu3_obj.__dict__)#解决代码重复问题使用函数#方案一#definit(obj,x,y,z):#obj.stu_name=x#obj.stu_age=y#obj.stu_gender=z###init(stu1_obj,carter,18,male)#init(stu2_obj,jack,20,male)#init(stu3_obj,mary,21,female)#方案二#先定义类classStudent:#1)变量的定义stu_schools=world#在类调用阶段运行#空对象,(carter,18,male)def__init__(obj,x,y,z):obj.stu_name=x#空对象.stu_name=carterobj.stu_age=y#空对象.stu_age=18obj.stu_gender=z#空对象.stu_gender=male#2)功能的定义defset_info(stu_obj,x,y,z):stu_obj[stu_name]=xstu_obj[stu_age]=ystu_obj[stu_gender]=z#print(====)#二再调用类产生对象#调用类的过程又称之为实例化#1.先产生一个空对象#2,python会自动调用类中的__init__方法#将空对象已经调用类是括号传入的参数传入给__init__方法#3.返回初始化完的对象stu1_obj=Student(carter,18,male)stu2_obj=Student(jack,20,male)stu3_obj=Student(mary,21,female)print(stu1_obj.__dict__)#总结__init__方法#1.会在调用类时自动触发执行,用来为对象初始化自己独有的数据#2.__init__应该存放对象初始化属性的功能,但是也是可以存放其他的#代码的,可以存放想要在类调用时立刻执行的代码#3.init方法返回None3.属性查找
#-*-coding:utf-8-*-classStudent:#1)变量的定义stu_schools=world#在类调用阶段运行#空对象,(carter,18,male)def__init__(self,x,y,z):self.stu_name=x#空对象.stu_name=carterself.stu_age=y#空对象.stu_age=18self.stu_gender=z#空对象.stu_gender=male#2)功能的定义defset_info(self,x,y,z):self.stu_name=xself.stu_age=yself.stu_gender=zdeffunc(self):print(helloworld)#print(====)stu1_obj=Student(carter,18,male)stu2_obj=Student(jack,20,male)stu3_obj=Student(mary,21,female)print(stu1_obj.__dict__)#类中存放的对象共有数据与功能的#一、类可以访问#1.类的数据属性共享给所有数据#print(Student.stu_schools)#2.类的函数属性#print(Student.set_info)#二、类中的东西是给对象用的#1.类的数据属性是共享给所有对象用的,大家访问的地址都一样#print(stu1_obj.stu_name)#print(stu1_obj.stu_age)#print(stu1_obj.stu_gender)#print(stu1_obj.stu_schools)#2、类的函数属性是绑定给对象使用的#绑定方法的特殊之处在于:谁调用绑定方法就会将谁当作第一个参数自动传入#print(Student.set_info)#严格按照函数的规则来用#Student.set_info(stu1_obj,carter,20,male)#绑定方法的特殊之处在于:谁调用绑定方法就会将谁当作第一个参数自动传入#其他严格按照函数的规则使用#在类中新定一个函数一定要传入一个参数用来接收绑定方法传入的参数self#stu1_obj.set_info(carter,20,male)#set_info(stu1_obj,carter,20,male)
#-*-coding:utf-8-*-#面向过程defadd_user():passdefdel_user():passdefmodify_user():passdefquery_user():passdefshow_all():passdefstart():whileTrue:print("""---------------------------名片管理系统V1.01:添加名片2:删除名片3:修改名片4:查询名片5:显示所有名片6:退出系统---------------------------""")operator=input(请输入要进行的操作(数字))ifoperator==1:add_user()elifoperator==2:del_user()elifoperator==3:modify_user()elifoperator==4:query_user()elifoperator==5:show_all()elifoperator==6:passelse:print(输入有误,请重新输入......)if__name__==__main__:start()4.面向对象的基本语法
#-*-coding:utf-8-*-#类和对象#小明今年18岁,身高1.75,每天早上跑完步,会去吃东西#小美今年17岁,身高1.65,小美不跑步,小美喜欢吃东西#1.定义类(驼峰命名)使用class来定义一个类#class类名# class类名:# class类名(object):classStudents:#类中要