[关闭]
@hainingwyx 2018-09-09T05:14:02.000000Z 字数 3371 阅读 1060

init 方法的继承

python


继承

Python同时支持单继承与多继承,语法如下:

  1. class SubClassName(ParentClass1 [, ParentClass2, ...]):
  2. class_suite

子类将继承父类的属性,也可以使用内建函数insubclass()来判断一个类是不是另一个类的子孙类

  1. class Parent(object):
  2. '''
  3. parent class
  4. '''
  5. numList = []
  6. def numdiff(self, a, b):
  7. return a-b
  8. class Child(Parent):
  9. pass
  10. c = Child()
  11. # subclass will inherit attributes from parent class
  12. #子类继承父类的属性
  13. Child.numList.extend(range(10))
  14. print(Child.numList)
  15. print("77 - 2 =", c.numdiff(77, 2))
  16. # built-in function issubclass()
  17. print(issubclass(Child, Parent))
  18. print(issubclass(Child, object))
  19. # __bases__ can show all the parent classes
  20. #bases属性查看父类
  21. print('the bases are:',Child.__bases__)
  22. # doc string will not be inherited
  23. #doc属性不会被继承
  24. print(Parent.__doc__)
  25. print(Child.__doc__)

输出结果:

  1. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. 77 - 2 = 75
  3. True
  4. True
  5. the bases are: (<class '__main__.Parent'>,)
  6. parent class
  7. None

__init__ 继承

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

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

输出结果:

  1. create an instance of: Child
  2. name attribute is: init Child

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

  1. def __init__(self, name):
  2. self.name = name
  3. print("create an instance of:", self.__class__.__name__)
  4. print("name attribute is:", self.name)
  5. #子类继承父类
  6. class Child(Parent):
  7. #子类中没有显示调用父类的初始化函数
  8. def __init__(self):
  9. print("call __init__ from Child class")
  10. #c = Child("init Child")
  11. #print()
  12. #将子类实例化
  13. c = Child()
  14. print(c.name)

输出结果:

  1. call __init__ from Child class
  2. Traceback (most recent call last):
  3. ...此处省略若干
  4. AttributeError: 'Child' object has no attribute 'name'

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

  1. class Parent(object):
  2. def __init__(self, name):
  3. self.name = name
  4. print("create an instance of:", self.__class__.__name__)
  5. print("name attribute is:", self.name)
  6. class Child(Parent):
  7. def __init__(self):
  8. print("call __init__ from Child class")
  9. super(Child,self).__init__("data from Child") #要将子类Child和self传递进去
  10. #c = Child("init Child")
  11. #print()
  12. d = Parent('tom')
  13. c = Child()
  14. print(c.name)

输出结果:

  1. #实例化父类Parent的结果
  2. create an instance of: Parent
  3. name attribute is: tom
  4. #实例化子类Child的结果
  5. call __init__ from Child class
  6. #super首先会先使得父类初始化的参数进行实例化
  7. create an instance of: Child
  8. name attribute is: data from Child
  9. data from Child

super

super主要来调用父类方法来显示调用父类,在子类中,一般会定义与父类相同的属性(数据属性,方法),从而来实现子类特有的行为。也就是说,子类会继承父类的所有的属性和方法,子类也可以覆盖父类同名的属性和方法。

  1. class Parent(object):
  2. Value = "Hi, Parent value"
  3. def fun(self):
  4. print("This is from Parent")
  5. #定义子类,继承父类
  6. class Child(Parent):
  7. Value = "Hi, Child value"
  8. def ffun(self):
  9. print("This is from Child")
  10. c = Child()
  11. c.fun()
  12. c.ffun()
  13. print(Child.Value)

输出结果:

  1. This is from Parent
  2. This is from Child
  3. Hi, Child value

可通过父类名直接访问父类的属性,需要将”self”显示传递:

  1. class Parent(object):
  2. Value = "Hi, Parent value"
  3. def fun(self):
  4. print("This is from Parent")
  5. class Child(Parent):
  6. Value = "Hi, Child value"
  7. def fun(self):
  8. print("This is from Child")
  9. Parent.fun(self) #调用父类Parent的fun函数方法
  10. c = Child()
  11. c.fun()

输出结果:

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

为了避免父类名硬编码到子类中:

  1. class Parent(object):
  2. Value = "Hi, Parent value"
  3. def fun(self):
  4. print("This is from Parent")
  5. class Child(Parent):
  6. Value = "Hi, Child value"
  7. def fun(self):
  8. print("This is from Child")
  9. #Parent.fun(self)
  10. super(Child,self).fun() #相当于用super的方法与上一调用父类的语句置换
  11. c = Child()
  12. c.fun()

输出结果:

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

参考文献

https://blog.csdn.net/brucewong0516/article/details/79121179

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注