【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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值