【python笔记】详解类class的继承、__init__初始化、super方法

1、 默认继承

在Python 3.x中取消了经典类,默认都是新式类,并且不必显式的继承object,也就是说:
class Person(object):pass
class Person():pass
class Person:pass

一样

2、继承中的_ _init_ _
当在Python中出现继承的情况时,一定要注意初始化函数_init_的行为:

  • 如果子类没有定义自己的初始化函数,父类的初始化函数会被默认调用;但是如果要实例化子类的对象,则只能传入父类的初始化函数对应的参数,否则会出错。
  • 如果子类定义了自己的初始化函数,而在子类中没有显示调用父类的初始化函数,则父类__init__内的属性不会被初始化
  • 如果子类定义了自己的初始化函数,在子类中显示调用父类初始化函数,子类和父类的__init__内属性都会被初始化

2.1、子类没有定义自己的初始化函数,父类的初始化函数会被默认调用:

#定义父类:Parent
class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)
#定义子类Child ,继承父类Parent       
class Child(Parent):
    pass
#子类实例化时,由于子类没有初始化,此时父类的初始化函数就会默认被调用
#且必须传入父类的参数name
c = Child("init Child") 

类实例化时,由于子类没有初始化,此时父类的初始化函数就会默认被调用,此时传入父类的参数name,输出结果为:

create an instance of: Child
name attribute is: init Child

如果不传入父类的参数name:

class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)

class Child(Parent):
    pass

#c = Child("init Child") 
#print()    
c = Child()

没有传入父类name参数的输出结果会报错:

TypeError: __init__() missing 1 required positional argument: 'name'

2.2、子类定义了自己的初始化函数,而在子类中没有显示调用父类的初始化函数,则父类的__init__内属性不会被初始化

class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)
#子类继承父类        
class Child(Parent):
    #子类中没有显示调用父类的初始化函数
    def __init__(self):
        print("call __init__ from Child class")
#c = Child("init Child") 
#print()  
#将子类实例化  
c = Child()
print(c.name)

在子类中没有显示调用父类的初始化函数,则父类的属性不会被初始化,因而此时调用子类中name属性不存在:
AttributeError: ‘Child’ object has no attribute ‘name’

AttributeError: 'Child' object has no attribute 'name'

2.3、如果子类定义了自己的初始化函数,显示调用父类,子类和父类的__init__内属性都会被初始化

class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)

class Child(Parent):
    def __init__(self):
        print("call __init__ from Child class")
        super(Child,self).__init__("data from Child")   #要将子类Child和self传递进去
#c = Child("init Child") 
#print() 
d = Parent('tom')   
c = Child()
print(c.name)

子类定义了自己的初始化函数,显示调用父类,子类和父类的属性都会被初始化的输出结果:

#实例化父类Parent的结果
create an instance of: Parent
name attribute is: tom

#实例化子类Child的结果
call __init__ from Child class
#super首先会先使得父类初始化的参数进行实例化
create an instance of: Child
name attribute is: data from Child
data from Child

3、super的使用详解

  • super主要来调用父类方法来显示调用父类,在子类中,一般会定义与父类相同的属性(数据属性,方法),从而来实现子类特有的行为。也就是说,子类会继承父类的所有的属性和方法,子类也可以覆盖父类同名的属性和方法
class Parent(object):
    Value = "Hi, Parent value"
    def fun(self):
        print("This is from Parent")
#定义子类,继承父类               
class Child(Parent):
    Value = "Hi, Child  value"
    def ffun(self):
        print("This is from Child")

c = Child()    
c.fun()
c.ffun()
print(Child.Value)

输出结果:

This is from Parent
This is from Child
Hi, Child value

但是,有时候可能需要在子类中访问父类的一些属性,可以通过父类名直接访问父类的属性,当调用父类的方法时,需要将”self”显示的传递进去的方式

class Parent(object):
    Value = "Hi, Parent value"
    def fun(self):
        print("This is from Parent")

class Child(Parent):
    Value = "Hi, Child  value"
    def fun(self):
        print("This is from Child")
        Parent.fun(self)   #调用父类Parent的fun函数方法

c = Child()    
c.fun()

输出结果:

This is from Child
This is from Parent  #实例化子类Child的fun函数时,首先会打印上条的语句,再次调用父类的fun函数方法

这种方式有一个不好的地方就是,需要经父类名硬编码到子类中,为了解决这个问题,可以使用Python中的super关键字:

class Parent(object):
    Value = "Hi, Parent value"
    def fun(self):
        print("This is from Parent")

class Child(Parent):
    Value = "Hi, Child  value"
    def fun(self):
        print("This is from Child")
        #Parent.fun(self)
        super(Child,self).fun()  #相当于用super的方法与上一调用父类的语句置换

c = Child()    
c.fun()

输出结果:

This is from Child
This is from Parent  #实例化子类Child的fun函数时,首先会打印上条的语句,再次调用父类的fun函数方法
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值