python学习笔记-继承

本文介绍了Python中的继承概念,包括经典类与新式类的区别,单继承、多继承以及重写父类方法的实现。通过实例展示了如何创建子类并调用父类方法,同时讲解了如何处理同名属性和方法。此外,还探讨了私有属性的使用以及通过getter和setter方法修改私有属性。文章最后提到了`super()`函数的使用,以及如何在子类中调用父类的方法。
摘要由CSDN通过智能技术生成

python学习笔记-继承

1. 经典类与新式类

经典类:

Class 类名:
	代码

新式类:

Class 类名(object):
	代码

Object:所有类的顶级类 ,括号里面可以是其他的父类

2. 继承的写法

子类默认继承父类所有的方法以及属性 Object是顶级类

	class A(object):
	
	    def __init__(self):
	        self.num = 1
	
	    def info_print(self):
	        print(self.num)
	
	
	
	class B(A):
	    pass
	
	
	result = B()   #创建B的对象
	result.info_print()  #由于继承关系  所以B的对象可以调用A的方法
	
	

3. 单继承

	class Master(object):
	
	    def __init__(self):
	        self.kongfu = '[煎饼果子配方]'
	
	
	    def make_cake(self):
	        print(f'运用{self.kongfu}制作煎饼果子')
	
	
	class Prentice(Master):
	    pass
	
	
	tudi = Prentice()
	print(tudi.kongfu)  #打印属性
	tudi.make_cake()

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(Master,School):
	    pass
	
	
	daqiu = Prentice()
	print(daqiu.kongfu)
	daqiu.make_cake()

5. 重写

	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__)   #快速查看继承关系

6. 子类调用父类同名的方法和属性

Prentice重写了父类的属性以及方法, 子类的属性与方法和父类的属性以及方法都是相同的,所以当我调用make_cake方法时,肯定调用的是子类的方法,所以为了能够调用父类的方法,在子类中定义两个方法,分别为:make_master_cake以及make_school_cake方法,这两个方法用来调用父类的方法,但是为了保证调用的是父类的属性,必须在调用前进行父类的初始化那么如果我要调用子类的Make_cake方法(事先已经调用了make_master_cake方法)会不会得到理想的结果,不会!因为父类属性会覆盖子类的属性,所以,必须在调用方法前进行初始化,

	#子类调用父类同名的方法和属性
	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()
	print(daqiu.kongfu)
	daqiu.make_cake()
	print(Prentice.__mro__)#快速查看继承关系
	
	
	daqiu.make_master_cake()

7. 多层继承

	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()
	print(daqiu.kongfu)
	daqiu.make_cake()
	print(Prentice.__mro__)#快速查看继承关系
	
	
	daqiu.make_master_cake()
	
	
	class tusun(Prentice):
	    pass
	
	s = tusun()
	s.make_cake()
	s.make_master_cake()
	s.make_school_cake()

8. super使用方法

  1. Super(当前类名,self).函数()
	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}制作煎饼果子')
	
	        super(School,self).__init__()
	        super(School,self).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)
	
	    #这里需要用一次  并且他的父类 school也要用一次 make_cake
	    def make_old_cake(self):
	        super(Prentice,self).__init__()
	        super(Prentice,self).make_cake()
	
	
	s = Prentice()
	s.make_old_cake()

  1. 无参数的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}制作煎饼果子')
	
	        # super(School,self).__init__()
	        # super(School,self).make_cake()
	
	        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)
	
	    #这里需要用一次  并且他的父类 school也要用一次 make_cake
	    def make_old_cake(self):
	        # super(Prentice,self).__init__()
	        # super(Prentice,self).make_cake()
	        super().__init__()
	        super().make_cake()
	
	
	s = Prentice()
	s.make_old_cake()

9. 私有属性

方法:在属性名和方法名前面添加两个下划线__ 保证该属性不会被子类进行直接调用

	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}制作煎饼果子')
	
	        # super(School,self).__init__()
	        # super(School,self).make_cake()
	
	        super().__init__()
	        super().make_cake()
	
	
	
	# 多继承    重写父类的重名方法 以及属性
	class Prentice(School):
	    def __init__(self):
	        self.kongfu = '[独创煎饼果子配方]'
	        self.__money = 200000  #私有属性
	
	    # 如果是先调用了父类的属性和方法  父类属性会覆盖子类属性   故在调用前  先调用自己子类的初始化
	    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)
	
	    #这里需要用一次  并且他的父类 school也要用一次 make_cake
	    def make_old_cake(self):
	        # super(Prentice,self).__init__()
	        # super(Prentice,self).make_cake()
	        super().__init__()
	        super().make_cake()
	
	
	class Q(Prentice):
	    pass

	s = Prentice()
	s.make_old_cake()
	
	
	q = Q()
	print(q.money)   #无法继承私有属性

修改私有属性: get set 方法

	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}制作煎饼果子')
	
	        # super(School,self).__init__()
	        # super(School,self).make_cake()
	
	        super().__init__()
	        super().make_cake()
	
	
	
	# 多继承    重写父类的重名方法 以及属性
	class Prentice(School):
	    def __init__(self):
	        self.kongfu = '[独创煎饼果子配方]'
	        self.__money = 200000  #私有属性
	
	    #获取私有属性和方法
	    def get_money(self):
	        return self.__money
	
	    def set_money(self):
	        self.__money  = 5000
	
	    # 如果是先调用了父类的属性和方法  父类属性会覆盖子类属性   故在调用前  先调用自己子类的初始化
	    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)
	
	    #这里需要用一次  并且他的父类 school也要用一次 make_cake
	    def make_old_cake(self):
	        # super(Prentice,self).__init__()
	        # super(Prentice,self).make_cake()
	        super().__init__()
	        super().make_cake()
	
	
	class Q(Prentice):
	    pass
	
	
	s = Prentice()
	s.make_old_cake()
	
	q = Q()
	print(q.get_money())   # 获取父类私有属性值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值