Subclass call the parent class method

When the same property or method this subclass is represented by its own property or method call,

super call parent class property or method.

When a property of a subclass called does not exist in the subclass, and then this call are super parent class property or method

1. manner a
sub-class method calls the parent class, comprising 2 in the form of calls. One form is a method by calling the parent class within a class inheritance manner, another form of the method is to call the parent class after the inheritance by way of the subclass is instantiated. As shown below:

Python_ subclass call the parent's method
Note that, within the sub-class attributes of the parent class when invoked by way of inheritance, we must bring self positional parameters (self only in the instantiation and when to invoke the property instance omitted (the original meaning of the self is the point to examples of their own )); and instances when a property or method inherited by calling the parent class, you do not pass the self argument, because the instance has been instantiated ah! ! !
The portion of the code block are as follows: note that the inner child property by calling the parent class inheritance manner, the parameters necessary to bring the position of self (self only during instantiation and in order to invoke the property instance omitted (self original meaning is pointing to examples of their own)); and instances when a property or method inherited by calling the parent class, you do not pass the self argument, because the instance has been instantiated ah! ! !

The portion of the code block are as follows:

class Person():
    "人的类"
    def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def eat(self): print("%s开始吃饭了"%self.name) class Student(Person): "学生类" def __init__(self,name,age,sex,classnaem): #子类调用父类的构造函数进行初始化 Person.__init__(self,name,age,sex) #通过子类把参数传给父类(self不能少,self只有在实例化和实例调用类时才能省略,此处不是) self.classnaem = classnaem def course(self): print("%s在%s上课"%(self.name,self.classnaem)) def done(self): print("这个学生在干嘛?") #子类调用父类方法 Person.eat(self) #子类在调用父类方法必须要传self #实例化一个子类 student = Student("周明",23,'男',"11届土木3班") #调用子类本身的方法 student.course() #通过子类调用父类的方法--->实例化之后来调用父类的方法 student.eat() #调用子类的方法,在子类方法中调用了子类的方法,与student.eat有区别 student.done()

At this time, if the name of the parent class is changed, then all the places in the subclass used in the parent class names are to be modified. All such methods are not easy to post-maintenance, for which we have chosen the way two.
2. The second approach
used in place of the parent class name super, in order to facilitate later maintenance code, as shown below:

Python_ subclass call the parent's method
The benefits of using super, one can substitute the name of the parent, the parent class name change so late, we only need to change the name of the parent class name to the back of the subclass, the subclass other parent class name can not change; the second is after using super, you can not pass parameters self when you call the parent class method.
The portion of the code block is:

class Person():
    "人的类"
    def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def eat(self): print("%s开始吃饭了"%self.name) class Student(Person): "学生类" def __init__(self,name,age,sex,classnaem): #子类调用父类的构造函数进行初始化 # Person.__init__(self,name,age,sex) #通过子类把参数传给父类(self不能少,self只有在实例化和实例调用类时才能省略,此处不是) #使用super来替换父类名 super().__init__(name,age,sex) #通过子类把参数传给父类,用super可以省略self self.classnaem = classnaem def course(self): print("%s在%s上课"%(self.name,self.classnaem)) def done(self): print("这个学生在干嘛?") #子类调用父类方法 super().eat() #子类在调用父类方法,使用super可以省略self #实例化一个子类 student = Student("周明",23,'男',"11届土木3班") #调用子类本身的方法 student.course() #通过子类调用父类的方法--->实例化之后来调用父类的方法 student.eat() #调用子类的方法,在子类方法中调用了子类的方法,与student.eat有区别 student.done()


参考:https://www.cnblogs.com/a8457013/p/8438490.html

Guess you like

Origin www.cnblogs.com/klb561/p/11361585.html