一、理解面向对象
面向对象就是将编程当成是一个事物,对外界来说,事物是直接使用的,不用管它内部的情况。而编程就是设置事物能够做什么事。
二、类和对象
类和对象的关系;用类去创建一个对象
2.1面向对象实现方法
2.1.1定义类
经典类
class 类名():
代码
......
新式类
class 类名(object)
代码
三、烤地瓜案例
class SweetPotato():
def __init__(self):
self.cook_time=0
self.cook_static='生的'
self.condiments=[]
def cook(self,time):
self.cook_time += time
if 0<=self.cook_time<3:
self.cook_static='生的'
elif 3<=self.cook_time<5:
self.cook_static='半生不熟'
elif 5 <= self.cook_time < 8:
self.cook_static = '熟'
elif self.cook_time >= 8:
self.cook_static = '烤糊了'
def add_condiments(self,condiments):
self.condiments.append(condiments)
def __str__(self):
return f'这个地瓜考了{self.cook_time}分钟,状态是{self.cook_static},调料有{self.condiments}'
digua1=SweetPotato()
digua1.cook(2)
digua1.add_condiments('a')
print(digua1)
四、搬家具案例
需求:将小于房子剩余面积的家具摆放在房子里
1、定义类
家具类
class Furniture():
def __init__(self,name,area):
self.name=name
self.area=area
class Furniture():
def __init__(self,name,area):
self.name=name
self.area=area
class Home():
def __init__(self,address,area):
self.address=address
self.area=area
self.free_address=area
self.furniture=[]
def __str__(self):
return f'房子在{self.address}占地{self.area}剩余面积{self.free_address},家具有{self.furniture}'
def add_funiture(self, item):
if self.free_address>=item.area:
self.furniture.append(item.area)
self.free_address-=item.area
else:
print('家具太大塞不下')
bed=Furniture('双人床',6)
jia1=Home('a',1000)
jia1.add_funiture(bed)
print(jia1)
五、继承
python面向对象的继承指的是多个类之间的从属关系,即子类默认继承父类的所有属性和方法,具体如下
#父类A
class A(object):
def __init__(self):
self.num=1
def info_print(self):
print(self.num)
#子类B
class B(A):
pass
result=B()
result.info_print()
python中,所有类默认继承object类,object类是顶级类或基类;其他子类叫派生类
六、单继承
单一的继承关系
#父类A
class Master(object):
def __init__(self):
self.kongfu='[煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(Master):
pass
result=Prentice()
print(result.kongfu)
七、多继承
一个子类同时继承多个父类
当一个类有多个父类的时候,默认使用第一个父类的同名属性和方法
#父类A
class Master(object):
def __init__(self):
self.kongfu='[a煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[b煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(School,Master):
pass
result=Prentice()
print(result.kongfu)
result.info_print()
八、子类重写父类同名方法和属性
#父类A
class Master(object):
def __init__(self):
self.kongfu='[a煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[b煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[c煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
result=Prentice()
print(result.kongfu)
result.info_print()
结论:如果子类和父类拥有同名属性和方法,子类创建对象调用属性和方法的时候,调用的是子类的
print(Prentice.__mro__)
查看层级关系
九、子类调用父类的同名方法和属性
#父类A
class Master(object):
def __init__(self):
self.kongfu='[a煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[b煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[c煎饼果子配方]'
def info_print(self):
self.__init__()
#r如果先调用父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
print(f'运用{self.kongfu}制作煎饼果子')
def info_master_print(self):
Master.__init__(self)
Master.info_print(self)
def info_school_print(self):
School.__init__(self)
School.info_print(self)
result=Prentice()
print(result.kongfu)
result.info_master_print()
十、多层继承
#父类A
class Master(object):
def __init__(self):
self.kongfu='[a煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[b煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[c煎饼果子配方]'
def info_print(self):
#r如果先调用父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
print(f'运用{self.kongfu}制作煎饼果子')
def info_master_print(self):
Master.__init__(self)
Master.info_print(self)
def info_school_print(self):
School.__init__(self)
School.info_print(self)
class TUsun(Prentice):
pass
result=TUsun()
print(result.kongfu)
result.info_master_print()
十一、super()调用父类方法
法一、super(当前类名,self).函数()
法二、super().函数()
十二、私有权限
12.1定义私有属性和方法
私有权限即设置某个实例属性或实例方法不继承给子类
设置私有权限的方法:在属性名和方法名前面加上两个下划线__
#父类A
class Master(object):
def __init__(self):
self.kongfu='[a煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[b煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[c煎饼果子配方]'
self.__money=2
def __info_cin(self):
print(self.kongfu)
print(self.__money)
def info_print(self):
self.__init__()
#r如果先调用父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
print(f'运用{self.kongfu}制作煎饼果子')
def info_master_print(self):
Master.__init__(self)
Master.info_print(self)
def info_school_print(self):
School.__init__(self)
School.info_print(self)
class TUsun(Prentice):
pass
result=TUsun()
print(result.kongfu)
result.info_master_print()
print(result.__money)#无法访问
money为私有属性
12.2获取和修改私有属性值
在python中,一般定义函数名get_xx来获取私有属性,定义set_xx来修改私有属性值
#父类A
class Master(object):
def __init__(self):
self.kongfu='[a煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
class School(object):
def __init__(self):
self.kongfu = '[b煎饼果子配方]'
def info_print(self):
print(f'运用{self.kongfu}制作煎饼果子')
#子类B
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[c煎饼果子配方]'
self.__money=2
def __info_cin(self):
print(self.kongfu)
print(self.__money)
def get_money(self):
return self.__money
def set_money(self):
self.__money=20
def info_print(self):
self.__init__()
#r如果先调用父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
print(f'运用{self.kongfu}制作煎饼果子')
def info_master_print(self):
Master.__init__(self)
Master.info_print(self)
def info_school_print(self):
School.__init__(self)
School.info_print(self)
class TUsun(Prentice):
pass
result=TUsun()
print(result.kongfu)
result.info_master_print()
print(result.__money)#无法访问
在类的里面获取和修改