Some initialization problems inherited from the parent class when the subclass python

Direct look at the code:

class Person:
    def __init__(self):
        self.name = "jack"
class Student(Person):
    def __init__(self):
        self.school = " one China " 
STU = Student ()
 Print ( " The student's name is: " , stu.name)

At this point, the program is not running, and after being given the run:

Why is this?

__init__ equivalent python class constructor is called when the class is instantiated, subclasses, and when there is the parent class constructor, the subclass needs to display call parent class constructor. Note that in python constructor can not be overloaded when there exists more than one constructor, a default constructor calls the last. The modified code as follows:

class Person:
    def __init__(self):
        self.name = " Jack " 
class Student (the Person):
     DEF  __init__ (Self):
       . Super (Student, Self)   __init__ () 
        self.school = " one China " 
STU = Student ()
 Print ( " The student's name is: " , stu.name)

At this time, the result is normal.

super (Student, self). __init__ (): where the super keyword indicates that the parent class, Student is the name of this class, self refer to this class of their own.

How to set up private properties and methods in python?

The foregoing properties and methods plus __ (double underlined). When coupled with the private properties and methods can not be accessed outside the class, while the subclass inherits the properties and methods of the parent class, the subclass can not access private properties and methods of the parent class.

for example:

class Person:
    def __init__(self):
        Self. __name__ = " Jack " 
    DEF  __test (Self):
         Print ( " This is a private method of the parent class " )
 class Student (the Person):
     DEF  __init__ (Self):
        super(Student, self).__init__()
        super().__test()
        self.school = "一中"
stu = Student()

The code above will get an error:

To gain access to private properties and methods of the parent class, the need to do so:

class Person:
    def __init__(self):
        Self. __name__ = " Jack " 
        self.age = 12 is
     DEF  __test (Self):
         Print ( " It is the private methods of the parent class " )
     DEF Test (Self):
         Print ( " It is the public methods of the parent class " )
 class Student (the Person):
     DEF  __init__ (Self):
        super(Student, self).__init__()
        self.school = " one China " 
    DEF printStudentlnfo (Self):
         # own subclass inherits the parent class public property, direct access to 
        Print ( " Use common attributes of the parent class in a subclass: " , self.age)
         # Super () on behalf of the parent class can access the parent class public methods 
        # of course, should the sub-class does not override the parent class method can also be used self.test () to call 
        # Super and self difference: super is to refer to the parent class , self refers to the class itself 
        Super (). the Test ()
         # for private properties and methods of the parent class needs to be accessed by class name __ _ parent property or method name 
        # Super () ._ Person__test () 
        self._Person__test ( )
STU = Student ()
 Print ( " The student's name is: " , stu._Person__name)
 Print ( " age of the students is: " , stu.age)
stu._Person__test()
stu.printStudent()

Output:

To be clear python and no private modifier in the true sense, from the above code can also be seen. Python runtime property or method will be converted into __: __ _ class property or method name. In this manner you will have access to the private class property or method. Another way is by re class defined inside a public method, to call the private property or method, you can call this method when a public call subclass, which is one of the objects in the package is oriented role in The next will be combined introduced.

So assuming that the parent class constructor with parameters, how it should be initialized in a subclass of it?

Subclasses to call the parent class has a display configuration parameters, and passing the corresponding parameter in the initialization time, the specific code is as follows:

class Person:
    def __init__(self,name,age):
        self.name = name
        . Self __age = Age
     DEF  __test (Self):
         Print ( " It is the private methods of the parent class " )
     DEF Test (Self):
        . Self __test ()
         Print ( " It is the public methods of the parent class " )
     DEF the setAge (Self, Age):
        self.__age = age
    def getAge(self):
        return self.__age
class Student(Person):
    def __init__(self,school,name,age):
        super(Student, self).__init__(name=name,age=age)
        self.school = school
    def stuTest(self):
        super().test()
        Print ( " where the school is: " , self.school)
stu = Student("一中","tom",12)
stu.stuTest()
Print ( " The student's name is: " , stu.name)
 Print ( " age of the students is: " , stu.getAge ())

Output:

supplement:

Suppose constructor defined in the parent class is not shown, then the parent class constructor defined in subclass would not be displayed.

class the Person:
     DEF the Test (Self):
         Print ( " nothing " )
 class Student:
     DEF  __init__ (Self, name):
        self.name = name
STU = Student ( " tom " )
 Print ( " The name is: " , stu.name)

Output:

Guess you like

Origin www.cnblogs.com/xiximayou/p/12142888.html