目前流行的网页设计风格包括/长沙企业关键词优化
一、面向对象-继承
继承的概念
单继承
多继承
子类重写父类的同名属性和方法
子类调用父类的同名属性和方法
多层继承
super()
私有属性和私有方法
1.1继承的概念
所有类的顶级类object
class A(object):def __init__(self):self.num = 1def print_info(self):print(self.num)class B(A):passb = B()
b.print_info()
# 输出 1
1.2单继承
一个父类继承给一个子类。子类继承了父类,默认继承了父类里的所有属性和方法
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cookie(self):print(f'做煎饼{self.gongfu}')class Apprentice(Master):passpancake = Apprentice()
print(pancake.gongfu)
pancake.make_cookie()
1.3多继承
多继承就是一个类同时继承多个父类,在java中只支持单继承
当继承多个父类的时候,默认使用第一个参数父类的同名属性和同名方法
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cookie(self):print(f'摊煎饼{self.gongfu}')class School(object):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cookie(self):print(f'学校学习的摊煎饼{self.gongfu}')class Apprentice(School, Master):passpancake = Apprentice()
print(pancake.gongfu)
pancake.make_cookie()
# 输出
# 学校学习摊煎饼
# 学校学习的摊煎饼学校学习摊煎饼
1.4子类重写父类同名方法和属性
子类重写了父类的同名方法和属性
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cookie(self):print(f'摊煎饼{self.gongfu}')class School(object):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cookie(self):print(f'学校学习的摊煎饼{self.gongfu}')class Apprentice(School, Master):def __init__(self):self.gongfu = '这是徒弟自创的摊煎饼技术'def make_cookie(self):print(f'这是徒弟自创的做煎饼方法{self.gongfu}')pancake = Apprentice()
print(pancake.gongfu)
pancake.make_cookie()
# 输出
# 这是徒弟自创的摊煎饼技术
# 这是徒弟自创的做煎饼方法这是徒弟自创的摊煎饼技术
1.5拓展__mro顺序
打印出当前这个类,父类都有哪些以及父类的层级关系是什么
用法 类名.__mro
print(Apprentice.__mro__)
# 输出 (<class '__main__.Apprentice'>, <class '__main__.School'>, <class '__main__.Master'>, <class 'object'>)
1.6 子类调用父类的同名方法和同名属性
注意:在子类中的同名方法需要调用一次 self.__init__() 并且不用传self
定义的调用父类的方法时,需要初始化一次父类的init方法,并且都要传self,否则不知道是哪个对象调用的
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cake(self):print(f'师傅教的摊煎饼{self.gongfu}')class School(object):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cake(self):print(f'学校学习的摊煎饼{self.gongfu}')class Apprentice(School, Master):def __init__(self):self.gongfu = '这是徒弟自创的摊煎饼技术'# 注意 要调用一次 子类的初始化方法,保证每次是子类的初始化属性,并且不用传selfdef make_cake(self):self.__init__()print(f'这是徒弟自创的做煎饼方法{self.gongfu}')# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_Master_cake(self):Master.__init__(self)Master.make_cake(self)# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_School_cake(self):School.__init__(self)School.make_cake(self)pancake = Apprentice()
# print(pancake.gongfu)
pancake.make_cake()
pancake.make_Master_cake()
pancake.make_School_cake()
pancake.make_cake()
1.7多层继承
大于两层的层级关系叫多层,
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cake(self):print(f'师傅教的摊煎饼{self.gongfu}')class School(object):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cake(self):print(f'学校学习的摊煎饼{self.gongfu}')class Apprentice(School, Master):def __init__(self):self.gongfu = '这是徒弟自创的摊煎饼技术'# 注意 要调用一次 子类的初始化方法,保证每次是子类的初始化属性,并且不用传selfdef make_cake(self):self.__init__()print(f'这是徒弟自创的做煎饼方法{self.gongfu}')# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_master_cake(self):Master.__init__(self)Master.make_cake(self)# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_school_cake(self):School.__init__(self)School.make_cake(self)class TuSun(Apprentice):passpancake = TuSun()
# print(pancake.gongfu)
pancake.make_cake()
pancake.make_master_cake()
pancake.make_school_cake()
pancake.make_cake()
1.8 super() 调用父类方法
有两种方式,一种是带参数的,另一种是不带参数的
方法一,带参数的
super(当前类名,self).函数名()
super(Apprentice,self).__init__()
super(Apprentice,self).make_cake()
方法二,无参数的
super().函数名()
super().__init__()
super().make_cake()
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cake(self):print(f'师傅教的摊煎饼{self.gongfu}')class School(Master):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cake(self):print(f'学校学习的摊煎饼{self.gongfu}')super(School, self).__init__()super(School, self).make_cake()class Apprentice(School):def __init__(self):self.gongfu = '这是徒弟自创的摊煎饼技术'# 注意 要调用一次 子类的初始化方法,保证每次是子类的初始化属性,并且不用传selfdef make_cake(self):self.__init__()print(f'这是徒弟自创的做煎饼方法{self.gongfu}')# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_master_cake(self):Master.__init__(self)Master.make_cake(self)# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_school_cake(self):School.__init__(self)School.make_cake(self)def make_old_cake(self):super(Apprentice, self).__init__()super(Apprentice, self).make_cake()def make_new_cake(self):super().__init__()super().make_cake()pancake = Apprentice()
# print(pancake.gongfu)
pancake.make_old_cake()
pancake.make_new_cake()# 输出
# 学校学习的摊煎饼学校学习摊煎饼
# 师傅教的摊煎饼摊煎饼
# 学校学习的摊煎饼学校学习摊煎饼
# 师傅教的摊煎饼摊煎饼
1.9定义私有属性和方法
就是把某些属性和方法,设置为不继承给子类叫私有权限,能继承给子类的叫共有权限
设置私有权限的属性和方法,在前面加上下划线 __
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cake(self):print(f'师傅教的摊煎饼{self.gongfu}')class School(Master):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cake(self):print(f'学校学习的摊煎饼{self.gongfu}')super(School, self).__init__()super(School, self).make_cake()class Apprentice(School):def __init__(self):self.gongfu = '这是徒弟自创的摊煎饼技术'self.__monkey = 10000# 注意 要调用一次 子类的初始化方法,保证每次是子类的初始化属性,并且不用传selfdef make_cake(self):self.__init__()print(f'这是徒弟自创的做煎饼方法{self.gongfu}')# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_master_cake(self):Master.__init__(self)Master.make_cake(self)# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_school_cake(self):School.__init__(self)School.make_cake(self)def make_old_cake(self):super(Apprentice, self).__init__()super(Apprentice, self).make_cake()def make_new_cake(self):super().__init__()super().make_cake()def __print_info(self):self.__init__()print('我是私有方法')pancake = Apprentice()
# print(pancake.gongfu)
print(pancake.gongfu)
print(pancake.monkey)
pancake.print_info()# 输出
# AttributeError: 'Apprentice' object has no attribute 'monkey'
2.0获取和修改私有属性和方法
一般定义函数名为get_xx用来获取私有属性,定义set_xx用来修改私有属性
class Master(object):def __init__(self):self.gongfu = '摊煎饼'def make_cake(self):print(f'师傅教的摊煎饼{self.gongfu}')class School(Master):def __init__(self):self.gongfu = '学校学习摊煎饼'def make_cake(self):print(f'学校学习的摊煎饼{self.gongfu}')super(School, self).__init__()super(School, self).make_cake()class Apprentice(School):def __init__(self):self.gongfu = '这是徒弟自创的摊煎饼技术'self.__monkey = 10000# 注意 要调用一次 子类的初始化方法,保证每次是子类的初始化属性,并且不用传selfdef make_cake(self):self.__init__()print(f'这是徒弟自创的做煎饼方法{self.gongfu}')# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_master_cake(self):Master.__init__(self)Master.make_cake(self)# 需要调用父类的初始化方法,并且都要传self,否则找不到是哪个对象调用的def make_school_cake(self):School.__init__(self)School.make_cake(self)def make_old_cake(self):super(Apprentice, self).__init__()super(Apprentice, self).make_cake()def make_new_cake(self):super().__init__()super().make_cake()def __print_info(self):self.__init__()print('我是私有方法')def get_monkey(self):return self.__monkeydef set_monkey(self):self.__monkey = 22222pancake = Apprentice()
# print(pancake.gongfu)
print(pancake.get_monkey())
pancake.set_monkey()
print(pancake.get_monkey())# 输出
# 10000
# 22222
一、继承总结
继承的特点
子类默认拥有父类的所有属性和方法
子类重写父类的同名方法和属性
子类调用父类的同名方法和属性
super()方法快速调用父类的方法
带参数写法
super(当前类名,self).方法名()
不带参数写法
super().方法名()
私有权限
不能继承给子类的属性和方法
语法
class 类名():
# 私有属性
__属性名 = 属性值
# 私有方法
def __函数名(self):
代码
获取属性名方法
get_xx用来获取私有属性
修改属性名方法
定义set_xx用来修改私有属性