Python_ method for calling the parent class of a subclass

Source: Internet
Author: User

1. Mode One
A subclass invokes a method of a parent class that contains a call in the form of 2. One form is to invoke the method of the parent class within the class through inheritance, and the other is to invoke the method of the parent class by inheritance after the subclass is instantiated. As shown in the following:

Note that when you call the properties of a parent class in an inherited way inside a subclass, you must bring the self positional argument (self is only omitted during instantiation and when the instance invokes the property), and the instance calls the property or method of the parent class through inheritance. You do not need to pass the self parameter, because the instance has been instantiated!!!
The code block for this section is as follows: Note that when you invoke the properties of a parent class internally through inheritance, you must take the self positional argument (self is only omitted during instantiation and when the instance invokes the property) While the instance invokes the property or method of the parent class through inheritance, it does not have to pass the self argument because the instance has already been instantiated!!!

The code block for this section is as follows:

  class Person (): "Human Class" Def __init__ (self,name,age,sex): self.name = name Self.age = Age Self.sex = Sex def eat (self): print ("%s starts eating"%self.name) class Student (person): "Student Class" Def __init__ (self, NAME,AGE,SEX,CLASSNAEM): #子类调用父类的构造函数进行初始化 person.__init__ (self,name,age,sex) #通过子类把参数传给父类 (self not less, self only in real Instantiation and instance invocation class can be omitted, not here) Self.classnaem = Classnaem def course (self): print ("%s classes in%s"% (Self.name,self.classnae m) def done: print ("What is this student doing?") ") #子类调用父类方法 person.eat (self) #子类在调用父类方法必须要传self # instantiates a subclass Student = Student (" Zhou Ming ", 23, ' Male '," 11 Civil 3 classes ") #调用子类本身的方 Method Student.course () #通过子类调用父类的方法---> Instantiation of the Method Student.eat () #调用子类的方法 that later called the parent class, the method that called the subclass in the subclass method, differs from Student.eat student.done ()  

At this point, if the name of the parent class changes, all the names of the parent classes in the subclass are modified. All of this method is not convenient for post-maintenance, for which we choose Mode Two.
2. Mode two
uses Super to replace the parent class name to facilitate post-maintenance of the code, see:

The advantage of using super, one can replace the parent class name, so that the late parent class name changes, we just need to change the subclass name after the parent class name, The other parent class names in the subclass can be used without changes, and the second is that when super is called, the parent class method can be invoked without argument self. The code block for this section of
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()

Python_ method for calling the parent class of a subclass

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.