python 类的继承

一、父类、子类

继承如何表示:

class 子类名(父类名):

            pass

1)子类通过继承可以获得父类的属性和方法

class Phone:

    def __init__(self, number):
        self.number = number

    def call(self, toWho, record=False):  # 默认不录音
        """打电话"""
        print('本机号:{}正在给{}打电话'.format(self.number, toWho))
        if record:
            self.record()

    def record(self):
        print('打电话的时候,正在录音')

#智能手机
class smartPhone(Phone):#子类smartPhone继承父类Phone的方法和属性
    pass

class IPhone():
    pass

#初始化一个普通手机
normal_phone = Phone('16654546765')
print(normal_phone.call('LiLi'))
print(normal_phone.number)

#初始化一个智能手机huawei
huawei = smartPhone('18678787878')#smartPhone继承了父类Phone的__init__,这里需要传入number的参数
print(huawei.call('zjx'))
print(huawei.number) 

'''   本机号:16654546765正在给LiLi打电话
  None  
  15866668888  
  本机号:
18678787878正在给zjx打电话  
  None 
  13988886666
'''

2)父类不能调用子类方法,单边的

class Phone:

    def __init__(self, number):
        self.number = number

    def call(self, toWho, record=False):  # 默认不录音
        """打电话"""
        print('本机号:{}正在给{}打电话'.format(self.number, toWho))
        if record:
            self.record()

    def record(self):
        print('打电话的时候,正在录音')

#智能手机
class smartPhone(Phone):

    def watch_moive(self,name):
        print('正在看{}电影')

class IPhone():
    pass

#父类不能调用子类的方法
normal_Phone = Phone('18821213434')
print(normal_Phone.watch_moive('八佰'))
'''
AttributeError: 'Phone' object has no attribute 'watch_moive'
'''

3)子类可以实现自己独有的方法,子类可以覆盖父类方法==>重写

class Phone:

    def __init__(self, number):
        self.number = number

    def call(self, toWho, record=False):  # 默认不录音
        """打电话"""
        print('本机号:{}正在给{}打电话'.format(self.number, toWho))
        if record:
            self.record()

    def record(self):
        print('打电话的时候,正在录音')

#智能手机
class smartPhone(Phone):

    def __init__(self,name,brand):
        self.name = name
        self.brand = brand

    def watch_moive(self,moive_name):
        print('用{}手机正在看电影:{}'.format(self.name,moive_name))

class IPhone():
    pass

#实例化一个智能手机对象oppo
oppo = smartPhone('oppo','30')#这里传入的参数必须是2个,子类重写父类的方法,子类调用必须用自己的
print(oppo.name)
print(oppo.brand)
print(oppo.watch_moive('《流浪地球》'))
'''
oppo
30
用oppo手机正在看电影:《流浪地球》
None
'''

4)当子类和父类具有相同的方法或者属性的时候,父类还是用自己的,子类不在用父类的而是用自己的

5)两层继承,子类没有的方法,可以去父类的父类找

class Phone:

    def __init__(self, number):
        self.number = number

    def call(self, toWho, record=False):  # 默认不录音
        """打电话"""
        print('本机号:{}正在给{}打电话'.format(self.number, toWho))
        if record:
            self.record()

    def record(self):
        print('打电话的时候,正在录音')

#智能手机
class SmartPhone(Phone):

    def __init__(self,name,brand):
        self.name = name
        self.brand = brand

    def watch_moive(self,moive_name):
        print('用{}手机正在看电影:{}'.format(self.name,moive_name))

class IPhone(SmartPhone):

    def __init__(self,number):
        self.number = number
        self.brand = '苹果8'

    def face_time(self):
        '''直播'''
        print('{}正在直播'.format(self.number))

    def call(self, toWho, record = False,face = False):
        '''打电话既可以录音  也可以facetime'''
        print('{}正在给{}打电话'.format(self.number,toWho))

        if record:
            self.record() #IPhone这个类里面没有record()方法,就往父类找,父类没有,就往父类的父类去找
        if face:
            self.face_time()

#实例化一个对象iPhone
iPhone = IPhone('19977776666')
print(iPhone.call('Lucy',True))

'''
19977776666正在给Lucy打电话
打电话的时候,正在录音
None
'''

 

二、super( ) 超继承

使用父类当中的方法

注意:super()函数只能是调用父类的方法   (不能越级调用父类的父类)

class YiFu:
    def sell(self):
        print('商场正在卖衣服')

class GirlYiFu(YiFu):
    def sell(self):
        # YiFu().sell() 如果父类方法需要传参数,就不方便了,要使用super
        super().sell()
        print('女装独特的卖衣服技巧')

normal = YiFu()
normal.sell()
nvzhuang = GirlYiFu()
nvzhuang.sell()     

'''
商场正在卖衣服
商场正在卖衣服
女装独特的卖衣服技巧
'''               
class Phone:

    def call(self,towho):
        pass

class SmartPhone(Phone):
    def call(self,towho,face = False):
        pass

class IPhone(SmartPhone):
    def call(self, towho, face=False):
        super().call(towho)   #只能调用父类的,不能越级调用
        print('正在给{}打电话'.format(towho))

#实例化一个对象
iPhone = IPhone()
print(iPhone.call('tudou',True))

总结:

1.super():子类使用super()函数,继承父类的方法,如果父类的方法里是传参数的,子类的方法也要正常传参数

2.super()函数可以简化代码,不用也是可以的。

3.继承父类,父类的方法就可以用。如果子类定义了(重写),就可以不用父类的方法

 

三、多重继承

一个子类可以同时继承多个父类,这多个父类的方法和属性,都可以访问

class SmartPhone:
    pass

class Camera:
    pass

class Recorder:
    pass

#Iphone这个子类同时继承上面3个父类
class Iphone(SmartPhone,Camera,Recorder):
    pass

 

扩展:属性的动态设置

1)对象.属性去获取属性

class Iphone:
    
    def __init__(self,number):
        self.number = number
#实例化一个对象iphone
iphone = Iphone('18831114545')
print(iphone.number)   #18831114545

2)getattr()的内置函数:  用于动态返回一个对象属性值

class Iphone:
    def __init__(self,number):
        self.number = number

#实例化一个对象iphone
iphone = Iphone('18831114545')
print(iphone.number) #18831114545
#动态获取该对象的属性
print(getattr(iphone,'number')) #18831114545,这里的number是字符串

3) getattr()动态设置不存在的属性

class Iphone:
    def __init__(self,number):
        self.number = number

# 实例化一个对象iphone
iphone = Iphone('18831114545')
print(iphone.number)  #18831114545
# 动态获取该对象的属性
print(getattr(iphone,'number'))   #18831114545,这里的number是字符串
# 动态设置不存在的属性
print(getattr(iphone,'brand','苹果'))     # 苹果
print(iphone.brand)    # 会报错。AttributeError: 'Iphone' object has no attribute 'brand'

4)动态属性赋值给变量    可以将属性定义为变量传入

class Iphone:
    def __init__(self,number):
        self.number = number

# 实例化一个对象iphone
iphone = Iphone('18831114545')
print(iphone.number)  #18831114545
# 动态获取该对象的属性
print(getattr(iphone,'number'))   #18831114545,这里的number是字符串
# 属性赋值给变量,通过变量获取属性
prop_name = 'brand' 
print(getattr(iphone,prop_name,'苹果'))  # 苹果

5)setattr():设置属性

class Iphone:
    def __init__(self,number):
        self.number = number

#实例化一个对象iphone
iphone = Iphone('18831114545')
print(iphone.number)  #18831114545
#动态设置属性
setattr(iphone,'number','16631102424') #等价于iphone.numbe = '999',但是这个方法是不能用变量的
print(iphone.number)  #16631102424
#设置不存在的属性
setattr(iphone,'color','white') #访问设置的属性
print(iphone.color)  #white

getattr():动态获取属性(已经存在的);动态设置不存在的属性,返回默认值

setattr():动态设置不存在的属性,设置完该属性,该属性就会存在,可以进行访问

 

posted @ 2020-09-10 17:45  尐樣ル~  阅读(175)  评论(0编辑  收藏  举报