coding:utf-8
当前系统日期:2022/8/16
面向对象特性:封装、继承、多态
创建类默认继承基础类object
class Father(object):
_f_money = '一千亿'
charactor = '开朗'
def __init__(self,aihao):
self.aihao = aihao
def hobby(self):
print(f'爱好是 {self.aihao}')
def ability(self):
print('聪明人')
class Me(Father):
pass # 保持结构的完整性
xiaoming = Me('read')
print(xiaoming._f_money)
子类继承父类,拥有父类所有的属性和方法;_可以被继承;__强制私有,不能被继承
class Mother():
_feature = '美貌'
charactor = '内向'
class son(Father, Mother): # 多继承,有相同的属性或方法,从左到右顺序继承
pass
xiaogang = son('read')
print(xiaogang.charactor)
print(son.__mro__) # 打印继承顺序
子类重写父类的同名方法和属性
class My(Father):
def ability(self): # 重写父类的方法
print('正常人')
def hidden_ability(self):
Father.ability(self) # 在需要的情况下,使用父类的方法或属性
子类调用父类的同名方法和属性
xiaohua = My('read')
xiaohua.ability()
super() 调用父类的方法
class daughter(Father):
def __init__(self, hobby):
super().__init__(hobby)
xiaofang = daughter('reading')
xiaofang.hobby()