How the python subclass inherits the instance variables of the parent class

  • 2021-09-24 22:51:21
  • OfStack

Type 1: Instance variables of both parent and child classes do not need to be passed


class A(object):
  def __init__(self):
    self.name = "cui"

  def get_name(self):
    return self.name


class B(A):
  def __init__(self):
    super(B,self).__init__()
    self.age = 12


b = B()

Type 2: The instance variables of the parent class do not need to be passed, and the instance variables of the subclass need to be passed


class A(object):
  def __init__(self):
    self.name = "zhang"


  def get_name(self):
    return self.name


class B(A):
  def __init__(self,age):
    super(B,self).__init__()
    self.age = age


b = B("san")

Type 3: Parent class has some instance variables to pass, and subclass instance variables to pass


class A(object):
  def __init__(self,sex):
    self.name = "zhang"
    self.sex = sex


  def get_name(self):
    return self.name + self.sex


class B(A):
  def __init__(self,sex,age):
    super(B,self).__init__(sex)
    self.age = age


b = B(" Male ",12)

Type 4: All variables of parent and child classes need to be passed


class A(object):
  def __init__(self,name,sex):
    self.name = name
    self.sex = sex


  def get_name(self):
    return self.name + self.sex


class B(A):
  def __init__(self,name,sex,age):
    super(B,self).__init__(name,sex)
    self.age = age


b = B("zhang"," Male ",12)

Type 5: Parent class variables need to be passed, and subclass has some instance variables to be passed


class A(object):
  def __init__(self,name,sex):
    self.name = name
    self.sex = sex


  def get_name(self):
    return self.name + self.sex


class B(A):
  def __init__(self,name,sex,age):
    super(B,self).__init__(name,sex)
    self.age = age
    self.courage = " High school "


b = B("zhang"," Male ",12)

Type 6: Both parent and child classes have partial instance variables to pass


class A(object):
  def __init__(self,name):
    self.name = name
    self.sex = " Female "


  def get_name(self):
    return self.name + self.sex


class B(A):
  def __init__(self,name,sex,age):
    super(B,self).__init__(name)
    self.age = age
    self.courage = " High school "


b = B("zhang"," Male ",12)

Summary:

The subclass inherits from the constructor of the parent class. In the constructor of the subclass, the variables in the __init__ method include all the variables that need to be passed by itself and the parent class, while the parameters in the super (). __init__ method include only the variables that need to be passed by the parent class


  def __init__(self,name,sex,age):
    super(B,self).__init__(name)
    self.age = age
    self.courage = " High school "

The above is how python subclass inherits the instance variables of the parent class in detail. For more information about python subclass inheriting the parent class variables, please pay attention to other related articles on this site!


Related articles: