简体   繁体   English

Python - 如何从子 class 实例调用超级 class 的方法?

[英]Python - How can I call methods of super class from child class instance?

When child class has a method with same name with the method in parent class, the method of child class overrides the method of parent class.当子 class 具有与父 class 中的方法同名的方法时,子 class 的方法将覆盖父 ZA2F2ED4F8EBC2CBBZ4 的方法。

In the definition of child class, it is possible to access the method of parent class by super() .在子 class 的定义中,可以通过super()访问父 class 的方法。

Then, Does it possible to access the method of parent class from child class Instance?那么,是否可以从子 class 实例访问父 class 的方法?

class Person:
    def print(self):
        print("Message from Person")

class Student(Person):
    def print(self):
        print("Message from Student")\

s = Student()
# Method of Student Class Instance
s.print()  # Output: "Message from Student"

# I want to call method "print" of Person
# from student Instance
# How can I call it?
s.super().print()  # ERROR
super(s).print()  # ERROR

If there is absolutely no way to access the parent class in any other way, I guess you could do this, but I would absolutely not recommend it:如果绝对没有办法以任何其他方式访问父 class,我想你可以这样做,但我绝对推荐它:

class Person:
    def print(self):
        print("Message from Person")


class Student(Person):
    def print(self):
        print("Message from Student")


s = Student()
# Method of Student Class Instance
s.print()  # Output: "Message from Student"
s.__class__.__base__.print(s.__class__.__base__)  # Output: "Message from Person"

You have to understand that this is super hacky, and that you are not calling the method of an instance, as it is the case with s.print() , but you are calling the method of an uninstantiated class, a method that is not a classmethod (or staticmethod for that matter) and pass it the class as an argument (ie the self argument).您必须了解这是超级hacky,并且您没有调用实例的方法,就像s.print()的情况一样,但是您正在调用未实例化的 class 的方法,该方法不是一个类方法(或就此而言的staticmethod方法)并将classmethod作为参数(即self参数)传递给它。 It's very different syntax.这是非常不同的语法。

And again, it makes much more sense to give the methods different names instead of overriding them.再一次,给方法赋予不同的名称而不是覆盖它们更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 我可以以编程方式从类实例中修饰类方法吗? - Python - can I programmatically decorate class methods from a class instance? 从父 class 实例创建子 class 实例,并从子 class 实例调用父方法 - Create child class instances from parent class instance, and call parent methods from child class instance 我们可以在python 2.7中从子类的类方法中调用父类的实例方法吗? - Can we call instance method of parent class from class method of child class in python 2.7? 实例属性可以在Python中调用实例类方法吗? - Can Instance Attributes Call Instance Class Methods in Python? 如何在同一类的旧实例上调用实例? - How can I call an instance on a former instance from the same class? 如何从另一个类继承实例参数/方法? - How can i inherit instance parameters /methods from another class? 隐藏子类用户的超级类方法 - Hiding Super Class Methods from User of Child Class 如何从子类中调用不同类的实例? - How to call instance of different class from child class? 如何从Python中的`super()`获取实例方法的下一个父类 - How do I get the instance method's next-in-line parent class from `super()` in Python 运行超级 class 方法后如何返回子 class 实例? - How do I return a child class instance after running a super class method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM