01 面向对象介绍
面向过程:
核心是"过程"二字
过程的终极奥义就是将程序流程化
过程是"流水线",用来分步骤解决问题的
面向对象:
核心是"对象"二字
对象的终极奥义就是将程序"整合"
对象是"容器",用来盛放数据与功能的
类也是"容器",该容器用来存放同类对象共有的数据与功能
python这门语言到底提供了什么语法来允许我们将数据与功能很好地整合好一起呢???
程序=数据+功能
学生的容器=学生的数据+学生的功能
课程的容器=课程的数据+课程的功能
粉扑、眼影、各种颜料=》原材料=》数据
眉笔、小刷子 =》工具 =》功能
学生的功能
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
stu_obj={
'stu_school':'oldboy',
'stu_name':'egon',
'stu_age':18,
'stu_gender':'male',
'tell_stu_info': tell_stu_info,
'set_info':set_info
}
stu1_obj={
'stu_school':'oldboy',
'stu_name':'lili',
'stu_age':19,
'stu_gender':'female',
'tell_stu_info': tell_stu_info,
'set_info':set_info
}
02 实现面向对象编程
一:先定义类
类 :是对象相似数据与功能的集合体。
所以类体中最常见的是变量与函数的定义,但是类体其实是可以包含任意其他代码的
注意:类体代码是在类定义阶段就会立即执行,会产生类的名称空间
class Student:
'''打印学生信息'''
# 1、变量的定义
stu_school = 'old_boy'
# 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('==== 类 类 类 ====')
'''类.__dict__:查类的属性和方法,输出如下:'''
print(Student.__dict__)
'''
{'__module__': '__main__',
'stu_school': 'old_boy',
'tell_stu_info': <function Student.tell_stu_info at 0x7f41adb7cd08>,
'set_info': <function Student.set_info at 0x7f41adb7cd90>,
'__dict__': <attribute '__dict__' of 'Student' objects>,
'__weakref__': <attribute '__weakref__' of 'Student' objects>, stu_)
'__doc__': None}
'''
# 1、访问数据属性
print(Student.stu_school) # 输出:old_boy ==> 等价于print(Student.__dict__['stu_school'])
# 2、访问函数属性
print(Student.tell_stu_info) # 输出:<function Student.tell_stu_info at 0x7f11d6b55d08>
# 等价于 print(Student.__dict__['tell_stu_info'])
# 3、新增属性,如:xx=123
Student.xx = 123
print(Student.__dict__) # 打印结果中新增了 'xx': 123,说明可以创造属性
二:再调用类产生对象
stu1_obj = Student()
stu2_obj = Student()
stu3_obj = Student()
'''注:上述也叫做实例化,创建一个对象,初始状态属性为空'''
print(stu1_obj.__dict__) # 输出:{}
print(stu2_obj.__dict__) # 输出:{}
print(stu3_obj.__dict__) # 输出:{}
1、为对象定制自己独有的属性,这里创建(或实例化)3个对象
stu1_obj.stu_name = 'linhui'
stu1_obj.stu_age = '19'
stu1_obj.stu_gender = 'male'
print(stu1_obj.__dict__) # 创建的属性:{'stu_name': 'linhui', 'stu_age': '19', 'stu_gender': 'male'}
stu2_obj.stu_name = 'dafeifei'
stu2_obj.stu_age = '20'
stu2_obj.stu_gender = 'male'
print(stu2_obj.__dict__) # 创建的属性:{'stu_name': 'dafeifei', 'stu_age': '20', 'stu_gender': 'male'}
stu3_obj.stu_name = "xiong"
stu3_obj.stu_age = '21'
stu3_obj.stu_gender = 'male'
print(stu3_obj.__dict__)
上述代码遗留两个问题
问题1:代码重复
问题2:属性的查找顺序
【解决问题1】
解决方案一:定义一个函数,将属性当做参数传给某个对象
def init(obj,x,y,z):
obj.stu_name = x
obj.stu_age = y
obj.stu_gender = z
init(stu1_obj, 'linhui','19','male')
print(stu1_obj.__dict__) # 创建stu1_obj的属性 {'stu_name': ('linhui',), 'stu_age': ('19',), 'stu_gender': 'male'}
解决方案二:
一:先定义类
class Student:
'''打印学生信息'''
# 1、变量的定义
stu_school = 'old_boy'
def __init__(obj, x, y, z): # 注:初始化函数名字必须为init,叫双下init(即:__init__)
pass
print(f'obj:{obj}')
obj.stu_name = x
obj.stu_age = y
obj.stu_gender = z
# return 123 # 会报错 TypeError: __init__() should return None, not 'int'
# return None # 默认只能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
print('==== 类 类 类 ====')
二:再调用类产生对象
==> 调用类的过程又称之为实例化,发生了三件事
1、先产生一个空对象
2、python会自动调用类中的__init__方法然将空对象已经调用类时括号内传入的参数一同传给__init__方法
3、返回初始完的对象
stu1_obj = Student('linhui', 18, 'male') # 等价于 Student.__init__(空对象,'linhui', 18, 'male')
stu2_obj = Student('dafeifei', 19, 'male')
stu3_obj = Student('xiong', 20, 'male')
print(stu1_obj.__dict__) # {'stu_name': ('linhui',), 'stu_age': (18,), 'stu_gender': 'male'}
print(stu2_obj.__dict__) # {'stu_name': ('dafeifei',), 'stu_age': (19,), 'stu_gender': 'male'}
print(stu3_obj.__dict__) # {'stu_name': ('xiong',), 'stu_age': (20,), 'stu_gender': 'male'}
三、总结__init__方法
1、会在调用类时自动触发执行,用来为对象初始化自己独有的数据
2、__init__内应该存放是为对象初始化属性的功能,但是是可以存放任意其他代码,想要在
类调用时就立刻执行的代码都可以放到该方法内
3、__init__方法必须返回None
03 属性查找
class Student:
'''打印学生信息'''
# 1、变量的定义
stu_school = 'old_boy'
count = 0
def __init__(self, x, y, z):
Student.count += 1
self.stu_name = x
self.stu_age = y
self.stu_gender = z
# return 123 # 会报错 TypeError: __init__() should return None, not 'int'
# return None # 默认只能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
print('==== 类 类 类 ====')
stu1_obj = Student('linhui', 18, 'male')
stu2_obj = Student('dafeifei', 19, 'male')
stu3_obj = Student('xiong', 20, 'male')
# print(dir(stu1_obj)) # 查看对象的所有属性和方法,返回的是列表
# print(stu1_obj.__dict__) # 查看对象的
# 计算调用类的次数
print(stu1_obj.count) # 输出 3 , 类的属性和对象的属性是共用的,实例化时会调用类,对象获取到的count也就是类本身的count
print(stu2_obj.count) # 输出 3
print(stu3_obj.count) # 输出 3
类中存放的是对象共有的数据与功能
一:类可以访问:
1、类的数据属性
print(Student.stu_school) # old_boy
print(stu1_obj.stu_school) # old_boy
2、类的函数属性
print(Student.tell_stu_info) # <function Student.tell_stu_info at 0x7f95f7f98d90>
print(Student.set_info) # <function Student.set_info at 0x7f95f7f98e18>
二:但其实类中的东西是给对象用的
1、类的数据属性是共享给所有对象用的,大家访问的地址都一样
print(id(Student.stu_school)) # 140406350253728
print(id(stu1_obj.stu_school)) # 140406350253728
print(id(stu2_obj.stu_school)) # 140406350253728
print(id(stu3_obj.stu_school)) # 140406350253728
Student.stu_school = 'DAFEIFEI'
print(f'111--stu1_obj.stu_school: {stu1_obj.stu_school}') # DAFEIFEI
stu1_obj.stu_school = 'DAFEIFEI'
print(f'222--stu1_obj.stu_school: {stu1_obj.stu_school}') # DAFEIFEI
2、类中定义的函数主要是给对象使用的,而且是绑定给对象的,虽然所有对象指向的都是相同的功能, 但是**绑定到不同的对象就是不同的绑定方法,内存地址各不相同。**
类调用自己的函数属性必须严格按照函数的用法来
print(Student.tell_stu_info) # <function Student.tell_stu_info at 0x7f2ff3188d90>
print(Student.set_info) # <function Student.set_info at 0x7f2ff3188e18>
Student.tell_stu_info(stu1_obj) # 学生信息:名字:('linhui',) 年龄:(18,) 性别:male
Student.tell_stu_info(stu2_obj) # 学生信息:名字:('dafeifei',) 年龄:(19,) 性别:male
Student.tell_stu_info(stu3_obj) # 学生信息:名字:('xiong',) 年龄:(20,) 性别:male
Student.set_info(stu1_obj, 'wukong', 1000, 'male')
Student.tell_stu_info(stu1_obj) # 学生信息:名字:('wukong',) 年龄:(1000,) 性别:male
3、绑定方法的特殊之处在于:谁来调用绑定方法就会将谁当做第一个参数自动传入
print(Student.tell_stu_info) # <function Student.tell_stu_info at 0x7f198ef54e18>
print(stu1_obj.tell_stu_info) # <bound method Student.tell_stu_info of <__main__.Student object at 0x7f198ef66c18>>
print(stu2_obj.tell_stu_info) # <bound method Student.tell_stu_info of <__main__.Student object at 0x7f198ef66c50>>
print(stu3_obj.tell_stu_info) # <bound method Student.tell_stu_info of <__main__.Student object at 0x7f198ef66c88>>
stu1_obj.tell_stu_info() #tell_stu_info(stu1_obj)
# 学生信息:名字:('linhui',) 年龄:(18,) 性别:male
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全栈开发
三、回顾我们使用过的内置函数,这里使用list举例。
- 打印 l1.append,发现append是某个列表对象的方法。内置函数list其实是一个类
- 使用l1.append(‘dd’)给列表添加元素,即对象l1调用了append方法
- 上述2中,等价于list.append(列表对象,‘元素’)
l1=['aa','bb','cc']
print(l1.append) # <built-in method append of list object at 0x7f7583313088>
l1.append('dd')
print(l1) # ['aa', 'bb', 'cc', 'dd']
list.append(l1, 'ee')
print(l1) # ['aa', 'bb', 'cc', 'dd', 'ee']
《附》: