@hainingwyx
2018-09-09T05:14:02.000000Z
字数 3371
阅读 1358
python
Python同时支持单继承与多继承,语法如下:
class SubClassName(ParentClass1 [, ParentClass2, ...]):class_suite
子类将继承父类的属性,也可以使用内建函数insubclass()来判断一个类是不是另一个类的子孙类
class Parent(object):'''parent class'''numList = []def numdiff(self, a, b):return a-bclass Child(Parent):passc = Child()# subclass will inherit attributes from parent class#子类继承父类的属性Child.numList.extend(range(10))print(Child.numList)print("77 - 2 =", c.numdiff(77, 2))# built-in function issubclass()print(issubclass(Child, Parent))print(issubclass(Child, object))# __bases__ can show all the parent classes#bases属性查看父类print('the bases are:',Child.__bases__)# doc string will not be inherited#doc属性不会被继承print(Parent.__doc__)print(Child.__doc__)
输出结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]77 - 2 = 75TrueTruethe bases are: (<class '__main__.Parent'>,)parent classNone
__init__ 继承子类没有定义自己的初始化函数,父类的初始化函数会被默认调用
#定义父类:Parentclass Parent(object):def __init__(self, name):self.name = nameprint("create an instance of:", self.__class__.__name__)print("name attribute is:", self.name)#定义子类Child ,继承父类Parentclass Child(Parent):pass#子类实例化时,由于子类没有初始化,此时父类的初始化函数就会默认被调用#且必须传入父类的参数namec = Child("init Child")
输出结果:
create an instance of: Childname attribute is: init Child
子类定义了自己的初始化函数,而在子类中没有显示调用父类的初始化函数,则父类的属性不会被初始化
def __init__(self, name):self.name = nameprint("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)
输出结果:
call __init__ from Child classTraceback (most recent call last):...此处省略若干AttributeError: 'Child' object has no attribute 'name'
如果子类定义了自己的初始化函数,显示调用父类,子类和父类的属性都会被初始化
class Parent(object):def __init__(self, name):self.name = nameprint("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: Parentname attribute is: tom#实例化子类Child的结果call __init__ from Child class#super首先会先使得父类初始化的参数进行实例化create an instance of: Childname attribute is: data from Childdata from Child
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 ParentThis is from ChildHi, 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 ChildThis is from Parent #实例化子类Child的fun函数时,首先会打印上条的语句,再次调用父类的fun函数方法
为了避免父类名硬编码到子类中:
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 ChildThis is from Parent #实例化子类Child的fun函数时,首先会打印上条的语句,再次调用父类的fun函数方法
https://blog.csdn.net/brucewong0516/article/details/79121179