我在b站学python之基础学习十五:继承

上一篇:我在b站学python之基础学习十四:面向对象基础(魔法方法)

一、体验继承

#定义父类
class A(object):
	def _init_(self):
		self.num = 1
	
	def info_print(self):
		print(self.num)

#定义子类,继承父类
class B(A):
	pass

#创建对象,验证结论
result = B()
result.info_print()

注意:在python中,所有类默认继承object类,object类是顶级类或者基类,其他子类叫派生类。

二、单继承

#师傅类,属性和方法
class Master(object):
	def _init_(self):
		self.kongfu = '[师父的]'
	
	def make_cake(self):
		print(f'{self.kongfu}')

#定义徒弟类,继承师傅类
class Prentice(Master):
	pass

#徒弟类创建对象,调用实例属性和方法
daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_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):
	pass

daqiu = Prentice()
print(daqiu.kongfu)
daqiu.make_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):
		print(f'{self.kongfu}')

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

注意:如果子类和父类拥有同名属性和方法,子类创建对象调用属性和方法的时候,调用到的是子类里边的同名属性和方法

五、快速查看类的继承关系
语法:类名.mro
例:
上边代码运行:print(Prentice.mro
结果:在这里插入图片描述

六、子类调用父类的同名方法和属性(方法一,方法二见下)
例:

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_()    #加自己初始化原因:如果不加,属性值是上一次调用的init内的属性值
		print(f'{self.kongfu}')
	
	#子类调用父类的同名方法和属性,把父类的同名属性和方法再次封装
	def make_master_cake(self):
		Master._init_(self)   #再次调用初始化的原因:想要调用弗雷德同名方法和属性,属性在init初始化位置,所以需要再次调用
		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()

七、多层继承
例:

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()调用父类方法(方法二)
例:
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()带参数写法
		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)
#方法一:super(当前类名,self).函数()
	def make_old_cake(self):
		super(Prentice,self)._init_()
		super(Prentice,self).make_cake()

daqiu = Prentice()
daqiu.make_old_cake()

2、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()无参数写法
		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)
#方法二:super().函数()
	def make_old_cake(self):
		super()._init_()
		super().make_cake()

daqiu = Prentice()
daqiu.make_old_cake()

九、私有权限
即设置某个实例属性或实例方法不继承给子类
1、设置私有权限方法:在属性名和方法名前加两个下划线。
例:

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 = '我的'
		#创建私有属性self.__money
		self.__money() = 20
	#定义私有方法
	def __info_print(self):
		print('私有方法')
	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()
#以上代码分别执行下列代码 都会报错
print(xiaoqiu.money)
print(xiaoqiu.__money)
xiaoqiu.__info_print()

2、获取和修改私有属性值
一般定义函数名get_xx用来获取私有属性,定义set_xx用来修改私有属性值。
例:

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 = '我的'
		self.__money = 20
	
	#定义函数,获取私有属性值
	def get_money(self):
		return self.__money
	
	#定义函数修改私有属性值
	def set_money(self):
		self.__money = 50
		
	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()

print(xiaoqiu.get_money())  #获取私有属性money的值
xiaoqiu.set_money()    #修改私有属性money的值
print(xiaoqiu.get_money())

下一篇:我在b站学python之基础学习十六:面向对象完

你可能感兴趣的:(python基础,python,学习,开发语言)