【Python】The importance of reloading the methods ingerited from the super classes

I have been always considering why should I reload the method that has been defined in the super classes.

However the answer on the Internet has always being like "When the function itself is different then we should rewrite the method of the super class".

But, today I met a very valid example that can help to explain why should we  rewrite the method of the super classes.

The code is like below:

There are two classes which were setted in the different files seperately - one is class Student(super class), the other is Colleage_class(sub class)

super class source code:

class Student(object):
    name1 = "Student_name222"
    num = 0
    def __init__(self, name ="", age = 1):
        self.age = 10
        self.name = "风清扬"
        if age < 0:
            self.age = 18
        else:
            self.age = age
        if name == "":
            self.name = "风清扬"
        else:
            self.name = name
        # Student.num += 1
        self.__class__.num += 1


    def print_file(self):
            if Student.num == 1:
                print("Now there are 1 student in the class")
            elif Student.num == 0:
                print("No student in class")
            else:
                print("Now there are {} students in class".format(Student.num))


    @staticmethod
    def cls_print(self):
        print("Now there are {} students in class".format(Student.num))
        print("self.name = ",self.name)

    @classmethod
    def cls_print1(cls):
        print("cls.num = ",cls.num)

def main():
    student1 = Student("xiaoming",32)
    student2 = Student("xiaoming", 32)
    student3 = Student("xiaoming", 32)
    student4 = Student("xiaoming", 32)
    Student.cls_print1()

if __name__ == "__main__":
    main()

sub class source code:
 

import sys
sys.path.append("E:\python_independent\review_object")
from review_object.object1 import Student


class Colleage_student(Student):
    def __init__(self, name = "xiaoming", age = 18, colleage_name = 'HNU', year = '1021', num = 0):
        super().__init__(name, age)
        self.colleage_name = colleage_name
        self.year = year
        self.num = num

    # @classmethod
    # def func(cls):
    #     print(cls.name, cls.age, cls.num)
    """上面被注释掉的这一段是有问题的,因为Colleage_student类中并没有类变量name,age,num"""
    def func(self):
        print(self.name, self.age, self.num)

def main():
    colleage_stu1 = Colleage_student("xiaoming", 18, "hnu", "213", 1)
    colleage_stu2 = Colleage_student("xiaoming", 18, "hnu", "213", 1)
    colleage_stu3 = Colleage_student("xiaoming", 18, "hnu", "213", 1)
    print("super class's class_variable num = ",Colleage_student.num)
    print("class Student's class_variable num = ",Student.num)
    colleage_stu1.func()
    colleage_stu1.print_file()


if __name__ == "__main__":
    main()

When I run the sub class then i found out that the method inherited from the super did not show the result that i wanted - From the code we can see it clearly that I have already added three students in the class Colleage_student,and the variable Colleage_student.num also showed that the num of current student in the class is 3, but the function - print_fille() which should have told us that there are 3 students in the class told us the wrong info - it said that there are no student in the class.

So we can safely draw the conclusion that we have to rewrite the method that have been inherited from the super class - print_file(),So the second edition of the class Colleage_student is as below:

import sys
sys.path.append("E:\python_independent\review_object")
from review_object.object1 import Student


class Colleage_student(Student):
    def __init__(self, name = "xiaoming", age = 18, colleage_name = 'HNU', year = '1021', num = 0):
        super().__init__(name, age)
        self.colleage_name = colleage_name
        self.year = year
        self.num = num

    # @classmethod
    # def func(cls):
    #     print(cls.name, cls.age, cls.num)
    """上面被注释掉的这一段是有问题的,因为Colleage_student类中并没有类变量name,age,num"""
    def func(self):
        print(self.name, self.age, self.num)

    def print_file(self):
        if Colleage_student.num == 0:
            print("There are no stundets in the class")
        elif Colleage_student.num == 1:
            print("There are only one student in the class")
        else:
            print("There are {} students in the class".format(Colleage_student.num))


def main():
    colleage_stu1 = Colleage_student("xiaoming", 18, "hnu", "213", 1)
    colleage_stu2 = Colleage_student("xiaoming", 18, "hnu", "213", 1)
    colleage_stu3 = Colleage_student("xiaoming", 18, "hnu", "213", 1)
    print("super class's class_variable num = ",Colleage_student.num)
    print("class Student's class_variable num = ",Student.num)
    colleage_stu1.func()
    colleage_stu1.print_file()
    student1 = Student()
    student1.print_file()


if __name__ == "__main__":
    main()

   Now it works well!

猜你喜欢

转载自blog.csdn.net/chenhanxuan1999/article/details/83039428
今日推荐