day22面向对象编程思想

时间:2024-05-01 17:57:19

day22面向对象编程思想

1、面向过程

面向过程:
核心是“过程”二字
过程的终极奥义就是将程序流程化
过程是“流水线”,用来分步骤解决问题的
面向对象:
核心是“对象”二字
对象的终极奥义及时将程序“整合”
对象是“容器”,用来盛放数据和功能的

1.1类的定义和对象的产生

'''
驼峰体命名:大驼峰,小驼峰
大驼峰:UserName ==> 类名推荐使用
小驼峰:userName
下划线的命名:user_name 除类名外推荐使用 类名的命名:大驼峰
class 类名()
pass 是先有类还是先有对象?
1.现实中:
先有对象,在有类
2.在程序中:
必须要先定义类,在调用类得到的对象 '''
定义类:
class Student():
school = "SH" def choose_course(sth_obj,course)
pass
print(123)
'''
定义类发生了几件事:
1.立即执行类体代码
2.产生一个类的名称空间,把类体中的属性名和函数名都扔到一个大字典中
3.把类的名称空间绑定给__dict__ 类名:__dict__ '''

2、类

代码1:
class Student:
stu_school = 'oldboy'
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(Student.stu_school) # 访问数据类型
print(Student.set_info) # 访问函数属性
代码2:
def init(obj,x,y,z):
obj.stu_name = x
obj.stu_age = y
obj.stu_gentre = z class Student:
stu_school = 'oldboy'
count = 0 def __init__(obj, x, y, z):
Student.count+=1
obj.stu_name = x
obj.stu_age = y
obj.stu_gentre = z 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('meng',18,'male')
print(stu1_obj.__dict__)
print(stu1_obj.count)
总结__init__方法
'''
1、会在调用时自动触发执行,用来为对象初始化自己独有的数据
2、__init__内应该存放是为对象初始化属性的功能,但是是可以存放任意其他代码,
想要在类调用时就立刻执行的代码都可以放到该方法内
3、__init__方法必须返回None
'''
代码3:
# 1. 绑定给对象的
class Student():
school = 'SH'
def __init__(self,name,age):
self.name =name
self.age = age # 在类中书写方法,默认绑定给对象使用的
def tell_info(self):
print('%s-%s'%(self.name,self.age)) def func(self):
print('func')
obj = Student('meng',18)
obj.tell_info()
obj.func()

3、property

案例1:
class People:
def __init__(self,name,weight,height):
self.name = name
self.weight = weight
self.height = height
@property #加上了就不用再调用了===》print(obj1.bmi())
def bmi(self):
return self.weight / (self.height ** 2) obj1 = People('meng',60,1.75)
print(obj1.bmi)
定义函数的原因:
1.从bmi的公示上看,bmi应该是触发功能计算得到的
2、bmi是随着身高、体重而动态变化的,不是一个固定的值,每次需要重新计算得到

4、继承

1、什么是继承
1)继承是一种创建新类的方式,新建的类可称为子类或者派生类,父类又可称为基类或超类,子类可以遗传父类的属性
2)需要注意的是:python支持多继承
3)在python中、新建的类可以继承一个或者多个父类
2、为什么要用继承
类解决的是对象与对象之间的代码冗余问题
用来解决类与类之间代码冗余问题 3、ps1:在python2中有经典类和新式类之分
新式类:继承了object类的子类,以及该子类的子子孙孙类
经典类:没有继承object类的子类,以及该子类的子子孙孙类
ps2:在python3中没有继承任何类,那么都会默认object类,所以python3中所有的类都是新式类
"""
学会子类如何使用父类的属性
1. 指名道姓:People.__init__(self, name, age, gender)
2. super():
"""
代码1:
class People():
school = 'SH' def __init__(self,name,age,genter):
self.name = name
self.age = age
self.genter = genter class Student(People):
def __init__(self,name,age,genter,course=None):
if course is None:
course =[]
self.course = course
People.__init__(self,name,age,genter) def choose_course(self,course):
self.course.append(course)
print('%s选课成功%s'%(self.name,self.course)) stu =Student('meng',19,'male')
print(stu.school)
print(stu.name)
print(stu.age)
print(stu.genter) # 补充:
class Teacher(People):
def __init__(self, name, age, genter, level):
self.level = level
People.__init__(self, name, age, genter) def score(self, stu_obj, score):
stu_obj.score = score
print('%s给%s打了%s分' % (self.name, stu_obj.name, score)) tea = Teacher('meng', 18, 'male', 11)
print(tea.name)
print(tea.age)
print(tea.genter)
print(tea.level)

5、单继承下的属性查找

例子1:
class Foo():
def f1(self):
print('Foo.f1') def f2(self):
print('Foo.f2') # 先输出
self.f1() class Bar(Foo):
def f1(self): # 后输出
print('Bar.f1') obj = Bar()
obj.f2()
结果:Foo.f2,Bar.f1 例子2:
class Foo:
def __f1(self):
print('Foo.f1') # 后执行 def f2(self):
print('Foo.f2') # 先执行
self.__f1() class Bar(Foo):
def f1(self):
print('Bar.f1') obj = Bar()
obj.f2()
# 总结:找一个值先在对象里找,然后再去类找

6、多继承下的属性

# 新式类中属性查找按照   广度优先查询
# 经典类中属性查找按照 深度优先查询 class G(object):
# def test(self):
# print('from G')
pass class F(G):
# def test(self):
# print('from F')
pass class C(F):
# def test(self):
# print('from C')
pass class D(G):
# def test(self):
# print('from D')
pass class E(G):
# def test(self):
# print('from E')
pass class H():
def test(self):
print('from H')
pass class B(E, H):
# def test(self):
# print('from B')
pass class A(B, C, D):
# def test(self):
# print('from A') pass f1 = A()
f1.test()

7、super()和mro()

class A:
def test(self):
print('A---->test')
super().aaa() class B:
def test(self):
print('B---->test') def aaa(self):
print('B---->aaa') class C(A, B):
def aaa(self):
print('C----->aaa') # c = C()
# c.test() # 打印结果:
# print(C.__mro__) a = A()
# a.test()
print(A.__mro__)

8、多态与多态性

# 多态:一种事物的多种形态
水:固态水,液态水, 气态水 动物:人,猪, 狗
# 继承, 子类使用父类的属性
多态类中的继承:不是让子类遗传父类的属性 多态带来的特性:在不考虑对象的数据类型的情况下,直接调用方法

9、绑定方法

一、绑定方法:特殊之处就在于将调用者本身当做第一个参数自动传入
1.绑定给对象的方法:调用者是对象,自动传入的是对象
2.绑定给类的方法:调用者类,自动传入的是类
3.应用场景:提供一种新的造对象的方式,来自于配置文件之类的
例子:
impore settings
class Mysql:
def __init__(self,ip,port):
self.ip = ip
self.port = port def func(self):
print('%s:%s'%(selp.op,selp.port))
@classmethod #将下面的函数装饰成绑定给类的方法
def from_conf(cls):
print(cls)
return cls(settings.ip,settings.port)
obj1 = Mysql(111,3303)

10.非绑定方法

一、非绑定方法===># 静态方法
用途:不加入任何装饰器的情况下,默认绑定给对象的。
不需要类和对象
class Mysql:
def __init__(self,ip,port):
self.nid = self.create_id() # 自动生成一个id
self.ip = ip
self.port = port
@staticmethod # 将下述函数装饰成一个静态方法
def create_id(x,y,z):
print(x,y,z)
import uuid
return uuid.uuid4()
obj1 = Mysql(111,3303)
print(Mysql.create_id)
Mysql.create_id(1,2,3)
obj1.create_id(4,5,6)