Python如何覆盖子级中的类成员并从父级访问它?

Mat*_*ias 4 python inheritance class

所以在Python中我有一个这样的类:

class Parent(object):
    ID = None

    @staticmethod
    def getId():
        return Parent.ID
Run Code Online (Sandbox Code Playgroud)

然后我覆盖子类中的ID,如下所示:

class Child(Parent):
    ID = "Child Class"
Run Code Online (Sandbox Code Playgroud)

现在我想打电话给 getId()孩子方法:

ch = Child()
print ch.getId()
Run Code Online (Sandbox Code Playgroud)

我现在想看"儿童班",但我得到"无".
我怎样才能在Python中实现这一目标?

PS:我知道我可以ch.ID直接访问,所以这可能更像是一个理论问题.

use*_*064 7

使用类方法:

class Parent(object):
    ID = None

    @classmethod
    def getId(cls):
        return cls.ID

class Child(Parent):
    ID = "Child Class"

print Child.getId() # "Child Class"
Run Code Online (Sandbox Code Playgroud)