核心是"对象"二字,对象的终极奥义就是将程序"整合"
对象是"容器",用来盛放数据与功能的
类也是"容器",该容器用来存放同类对象共有的数据与功能
用化妆作比喻,化妆所需要的原材料有:粉扑、眼影、各种颜料,这些
原材料相当于程序的数据;
化妆的工具有:眉笔、粉扑等,这些小工具
相当于程序的功能;而这些工具和原材料放在一起的彩妆盒就相当于一个对象。
1、首先需要定义类
类是对象相似数据与功能的集合体,所以类体中最常见的是变量与函数的定义,但是类体其实是可以包含任意其他代码的
注意:类体代码是在类定义阶段就会立即执行,会产生类的名称空间,区别于函数的定义,函数定义时不会执行,调用时才会执行。
class Student:
## 1、类的定义
class Student:
# 1、变量的定义
stu_school=‘oldboy‘
# 2、功能的定义
def tell_stu_info(stu_obj):
print(‘学生信息:名字:%s 年龄:%s 性别:%s‘ %(
stu_obj[‘stu_name‘],
stu_obj[‘stu_age‘],
stu_obj[‘stu_gender‘]
))
def set_info(stu_obj,x,y,z):
stu_obj[‘stu_name‘]=x
stu_obj[‘stu_age‘]=y
stu_obj[‘stu_gender‘]=z
print(‘========>‘)
print(Student.__dict__) #会先打印箭头,因为类在定义的时候就会立即执行
2.1 访问数据属性
print(Student.stu_school) # Student.__dict__[‘stu_school‘]
2.2、访问函数属性
print(Student.set_info) # Student.__dict__[‘set_info‘]
2.3 修改或增加属性
Student.x=1111 #Student.__dict__[‘x]=111
print(Student.__dict__)
stu1_obj=Student()
stu2_obj=Student()
stu3_obj=Student()
print(stu1_obj.__dict__) #获得的也是一个字典
print(stu2_obj.__dict__)
print(stu3_obj.__dict__)
stu1_obj.stu_name=‘egon‘ # stu1_obj.__dict__[‘stu_name‘]=‘egon‘
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=‘lili‘
stu2_obj.stu_age=19
stu2_obj.stu_gender=‘female‘
print(stu2_obj.__dict__)
stu3_obj.stu_name=‘jack‘
stu3_obj.stu_age=20
stu3_obj.stu_gender=‘male‘
print(stu2_obj.__dict__)
上述虽然可以完成为各个对象定义独有的属性,但存在的问题是:
问题1:代码重复,代码冗余
问题2:属性的没有确定的查找顺序,不知道是先从类找还是先从对象自身开始找
定义一个初始化对象的函数,通过调用函数传入不同的参数的方式来为不同的对象设置独有的属性,代码冗长的问题得到了解决,但是该函数并没有封装到类里,不符合面向对象的编程思想,没有将函数整合到类里面去。
def init(obj,x,y,z):
obj.stu_name=x
obj.stu_age=y
obj.stu_gender=z
init(stu1_obj,‘egon‘,18,‘male‘)
init(stu2_obj,‘lili‘,19,‘female‘)
init(stu3_obj,‘jack‘,20,‘male‘)
class Student:
# 1、变量的定义
stu_school=‘oldboy‘
# 空对象,‘egon‘,18,‘male‘
def __init__(obj,x,y,z):
obj.stu_name=x # 空对象.stu_name=‘egon‘
obj.stu_age=y # 空对象.stu_age=18
obj.stu_gender=z # 空对象.stu_gender=‘male‘
# return None
# 2、功能的定义
def tell_stu_info(stu_obj):
print(‘学生信息:名字:%s 年龄:%s 性别:%s‘ %(
stu_obj[‘stu_name‘],
stu_obj[‘stu_age‘],
stu_obj[‘stu_gender‘]
))
def set_info(stu_obj,x,y,z):
stu_obj[‘stu_name‘]=x
stu_obj[‘stu_age‘]=y
stu_obj[‘stu_gender‘]=z
注意:调用类的过程又称之为实例化,发生了三件事
1、先产生一个空对象
2、python会自动调用类中的__init__方法然将空对象以及调用类时括号内传入的参数一同传给__init__方法
3、返回初始完的对象
stu1_obj=Student(‘egon‘,18,‘male‘) # Student.__init__(空对象,‘egon‘,18,‘male‘)
stu2_obj=Student(‘lili‘,19,‘female‘)
stu3_obj=Student(‘jack‘,20,‘male‘)
print(stu1_obj.__dict__)
print(stu2_obj.__dict__)
print(stu3_obj.__dict__)
1、__init__会在调用类时自动触发执行,用来为对象初始化自己独有的数据
2、__init__内应该存放是为对象初始化属性的功能,但是是可以存放任意其他代码,想要在 类调用时就立刻执行的代码都可以放到该方法内
3、__init__方法必须返回None
跟上面一样先定义一个类:
class Student:
# 1、变量的定义
stu_school=‘oldboy‘
count=0
# 空对象,‘egon‘,18,‘male‘
def __init__(self,x,y,z):
Student.count += 1
self.stu_name=x # 空对象.stu_name=‘egon‘
self.stu_age=y # 空对象.stu_age=18
self.stu_gender=z # 空对象.stu_gender=‘male‘
# return None
# 2、功能的定义
def tell_stu_info(self):
print(‘学生信息:名字:%s 年龄:%s 性别:%s‘ %(
self.stu_name,
self.stu_age,
self.stu_gender
))
def set_info(self,x,y,z):
self.stu_name=x
self.stu_age=y
self.stu_gender=z
def choose(self,x):
print(‘正在选课‘)
self.course=x
调用类产生对象:
stu1_obj=Student(‘egon‘,18,‘male‘) # Student.__init__(空对象,‘egon‘,18,‘male‘)
stu2_obj=Student(‘lili‘,19,‘female‘)
stu3_obj=Student(‘jack‘,20,‘male‘)
之前我们说类是的是对象共有的数据于功能,这些数据于功能类自己能不能访问呢?我们知道类也是可以访问自身的数据属性于功能属性的
print(Student.stu_school) #oldboy
print(Student.tell_stu_info) #<function Student.tell_stu_info at 0x0000026D1B21A430>
print(Student.set_info) #<function Student.set_info at 0x0000026D1B21A4C0>
类是我们创建对象的时,只是让代码更加精简的一种机制,节省空间,其实类中的东西是给对象用的。
注意:类的数据属性是共享给所有对象用的,大家访问的地址都一样
print(id(Student.stu_school)) #1878565388080
print(id(stu1_obj.stu_school)) #1878565388080
print(id(stu2_obj.stu_school)) #1878565388080
print(id(stu3_obj.stu_school)) #1878565388080
#内存地址相同
Student.stu_school=‘OLDBOY‘ #改变类中的数据属性
print(Student.stu_school) #OLDBOY 类中的数据属性改变
print(stu1_obj.stu_school) #OLDBOY 对象访问到的数据属性已跟着改变
stu1_obj.stu_school=‘OLDBOY‘ #优先对象中查找,在对象的自己的名称空间将变量绑定了一个新的值的内存地址,没有对类产生影响。
print(Student.stu_school) #oldboy 类中的数据属性没有改变
print(stu1_obj.stu_school) #OLDBOY 对象自身的数据属性会改变
print(stu2_obj.stu_school) #oldboy 其他对象的数据属性没有改变
注意:类中定义的函数主要是给对象使用的,是绑定给对象的,虽然所有对象指向的都是相同的功能,但是绑定到不同的对象就是不同的绑定方法,内存地址各不相同,
类调用自己的函数属性必须严格按照函数的用法来
print(Student.tell_stu_info)
print(Student.set_info)
Student.tell_stu_info(stu1_obj)
Student.tell_stu_info(stu2_obj)
Student.tell_stu_info(stu3_obj)
Student.set_info(stu1_obj,‘EGON‘,19,‘MALE‘)
Student.tell_stu_info(stu1_obj
绑定方法的特殊之处在于:谁来调用绑定方法就会将谁当做第一个参数自动传入
print(Student.tell_stu_info)
print(stu1_obj.tell_stu_info)
print(stu2_obj.tell_stu_info)
print(stu3_obj.tell_stu_info)
stu1_obj.tell_stu_info() #tell_stu_info(stu1_obj)
stu2_obj.tell_stu_info() #tell_stu_info(stu2_obj)
stu3_obj.tell_stu_info() #tell_stu_info(stu3_obj)
stu1_obj.choose(‘python全栈开发‘)
print(stu1_obj.course)
#正在选课
python全栈开发
print(stu2_obj.course)
#AttributeError: ‘Student‘ object has no attribute ‘course‘
#报错对象stu2_obj没有course这个属性
stu1_obj.choose(‘python全栈开发‘)
print(stu1_obj.course)
print(stu2_obj.course)
stu2_obj.choose(‘linux运维‘)
print(stu2_obj.course)
stu3_obj.choose(‘高级架构师‘)
print(stu3_obj.course)
其实我们我们之前就已经用过类和对象了,比如list列表
l1=[‘aa‘,‘bb‘,‘cc‘] # l=list([1,2,3])
l2=[111,222,333] # l=list([1,2,3])
print(l1.append)
print(list.append)
l1.append(‘dd‘)
l2.append(‘dd‘)
print(l1)
print(l2)
list.append(l1,‘dd‘)
list.append(l2,‘dd‘)
print(l1)
print(l2)
原文:https://www.cnblogs.com/zhangtieshan/p/12657324.html