一、魔法方法

在Python中,

__xx__()

的函数叫做魔法方法,指的是具有特殊功能的函数。

1

__init__()

1.1 体验

__init__()
__init__()
方法的作用:初始化对象。
class Washer():
# 定义初始化功能的函数
def __init__(self):
# 添加实例属性
self.width = 500
self.height = 800
def print_info(self):
# 类里面调用实例属性
print(f'洗衣机的宽度是{self.width}, 高度是{self.height}')
haier1 = Washer()
haier1.print_info()

注意:

__init__()

方法,在创建一个对象时默认被调用,不需要手动调用

__init__(self)

中的self参数,不需要开发者传递,python解释器会自动把当前的对象引用传递过去。

1.2 带参数的

__init__()

思考:一个类可以创建多个对象,如何对不同的对象设置不同的初始化属性呢?

答:传参数。

class Washer():
def __init__(self, width, height):
self.width = width
self.height = height
def print_info(self):
print(f'洗衣机的宽度是{self.width}')
print(f'洗衣机的高度是{self.height}')
haier1 = Washer(10, 20)
haier1.print_info()
haier2 = Washer(30, 40)
haier2.print_info()
2
__str__()
当使用print输出对象的时候,默认打印对象的内存地址。如果类定义了
__str__
方法,那么就会打印从在这个方法中 return 的数据。
class Washer():
def __init__(self, width, height):
self.width = width
self.height = height
def __str__(self):
return '这是海尔洗衣机的说明书'
haier1 = Washer(10, 20)
# 这是海尔洗衣机的说明书
print(haier1)
3
__del__()
当删除对象时,python解释器也会默认调用
__del__()
方法。
class Washer():
def __init__(self, width, height):
self.width = width
self.height = height
def __del__(self):
print(f'{self}对象已经被删除')
haier1 = Washer(10, 20)
# <__main__.washer object at>对象已经被删除
del haier1

二、类的继承概念

继承是python面向对象的三大特性之一,是一种创建新类的方式,python中的继承,可以继承一个或者继承多个父类,新建的类被称之为派生类或者子类,被继承的类是父类,可以称之为基类,超类,继承是实现代码重用的重要方式。

生活中的继承,一般指的是子女继承父辈的财产。

拓展1:经典类或旧式类

不由任意内置类型派生出的类,称之为经典类。

class 类名:
代码
......
拓展2:新式类
class 类名(object):
代码
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() # 1
在Python中,所有类默认继承object类,object类是顶级类或基类;其他子类叫做派生类。
2.1 单继承
故事主线:一个煎饼果子老师傅,在煎饼果子界摸爬滚打多年,研发了一套精湛的摊煎饼果子的技术。师父要把这套技术传授给他的唯一的最得意的徒弟。
分析:徒弟是不是要继承师父的所有技术?
# 1. 师父类
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')

# 2. 徒弟类
class Prentice(Master):
pass


# 3. 创建对象daqiu
daqiu = Prentice()
# 4. 对象访问实例属性
print(daqiu.kongfu)
# 5. 对象调用实例方法
daqiu.make_cake()
2.2 多继承
故事推进:daqiu是个爱学习的好孩子,想学习更多的煎饼果子技术,于是,在百度搜索到黑马程序员,报班学习煎饼果子技术。
所谓多继承意思就是一个类同时继承了多个父类。
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


# 创建学校类
class School(object):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class Prentice(School, Master):
pass


daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()

注意:当一个类有多个父类的时候,默认使用第一个父类的同名属性和方法。

2.3子类重写父类同名方法和属性

故事:daqiu掌握了师父和培训的技术后,自己潜心钻研出自己的独门配方的一套全新的煎饼果子技术。

class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class School(object):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


# 独创配方
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_cake()

print(Prentice.__mro__)
子类和父类具有同名属性和方法,默认使用子类的同名属性和方法。
2.4 子类调用父类的同名方法和属性
故事:很多顾客都希望也能吃到古法和黑马的技术的煎饼果子。
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class School(object):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'

def make_cake(self):
# 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
self.__init__()
print(f'运用{self.kongfu}制作煎饼果子')

# 调用父类方法,但是为保证调用到的也是父类的属性,必须在调用方法前调用父类的初始化
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)

def make_school_cake(self):
School.__init__(self)
School.make_cake(self)


daqiu = Prentice()

daqiu.make_cake()

daqiu.make_master_cake()

daqiu.make_school_cake()

daqiu.make_cake()
三、多层继承
故事:N年后,daqiu老了,想要把所有技术传承给自己的徒弟。
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class School(object):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'

def make_cake(self):
self.__init__()
print(f'运用{self.kongfu}制作煎饼果子')

def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)

def make_school_cake(self):
School.__init__(self)
School.make_cake(self)


# 徒孙类
class Tusun(Prentice):
pass


xiaoqiu = Tusun()

xiaoqiu.make_cake()

xiaoqiu.make_school_cake()

xiaoqiu.make_master_cake()
四、super()调用父类方法
class Master(object):
def __init__(self):
self.kongfu = '[古法煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')


class School(Master):
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'

def make_cake(self):
print(f'运用{self.kongfu}制作煎饼果子')

# 方法2.1
# super(School, self).__init__()
# super(School, self).make_cake()

# 方法2.2
super().__init__()
super().make_cake()


class Prentice(School):
def __init__(self):
self.kongfu = '[独创煎饼果子技术]'

def make_cake(self):
self.__init__()
print(f'运用{self.kongfu}制作煎饼果子')

# 子类调用父类的同名方法和属性:把父类的同名属性和方法再次封装
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)

def make_school_cake(self):
School.__init__(self)
School.make_cake(self)

# 一次性调用父类的同名属性和方法
def make_old_cake(self):
# 方法一:代码冗余;父类类名如果变化,这里代码需要频繁修改
# Master.__init__(self)
# Master.make_cake(self)
# School.__init__(self)
# School.make_cake(self)

# 方法二: super()
# 方法2.1 super(当前类名, self).函数()
# super(Prentice, self).__init__()
# super(Prentice, self).make_cake()

# 方法2.2 super().函数()
super().__init__()
super().make_cake()


daqiu = Prentice()

daqiu.make_old_cake()
注意:使用super() 可以自动查找父类。调用顺序遵循
mro

类属性的顺序。比较适合单继承使用。