python继承和基类方法调用

发布于 2024-12-10 23:18:25 字数 694 浏览 0 评论 0原文

我希望基类中的方法调用同一类中的另一个方法,而不是继承类中的重写方法。 我想用下面的代码打印出

Class B: 6

Class A: 9

这可以吗?


# Base class definition
class ClassA(object):
    def __init__(self):
        print("Initializing A")

    # hoping that this function is called by this class's printFnX
    def fnX(self, x):
        return x**2

    def printFnX(self, x):
        print("ClassA:",self.fnX(x))

# Inherits from ClassA above
class ClassB(ClassA):
    def __init__(self):
        print("initizlizing B")

    def fnX(self, x):
        return 2*x

    def printFnX(self, x):
        print("ClassB:", self.fnX(x))
        ClassA.printFnX(self,x)

bx = ClassB()
bx.printFnX(3)

I would like a method in a base class to call another method in the same class instead of the overriding method in an inherited class.
I would like the following code to print out

Class B: 6

Class A: 9

Can this be done?


# Base class definition
class ClassA(object):
    def __init__(self):
        print("Initializing A")

    # hoping that this function is called by this class's printFnX
    def fnX(self, x):
        return x**2

    def printFnX(self, x):
        print("ClassA:",self.fnX(x))

# Inherits from ClassA above
class ClassB(ClassA):
    def __init__(self):
        print("initizlizing B")

    def fnX(self, x):
        return 2*x

    def printFnX(self, x):
        print("ClassB:", self.fnX(x))
        ClassA.printFnX(self,x)

bx = ClassB()
bx.printFnX(3)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

死开点丶别碍眼 2024-12-17 23:18:25

恭喜,您已经发现了 Python 双下划线名称修饰的激励用例:-)

有关详细信息和已完成的示例,请参阅:http://docs.python.org/tutorial/classes.html#private-variableshttp://docs.python.org/reference/expressions.html#atom-identifiers。

以下是如何在您的示例中使用它:

# Base class definition
class ClassA(object):
    def __init__(self):
        print("Initializing A")

    # hoping that this function is called by this class's printFnX
    def fnX(self, x):
        return x**2
    __fnX = fnX

    def printFnX(self, x):
        print("ClassA:",self.__fnX(x))

# Inherits from ClassA above
class ClassB(ClassA):
    def __init__(self):
        print("initizlizing B")

    def fnX(self, x):
        return 2*x

    def printFnX(self, x):
        print("ClassB:", self.fnX(x))
        ClassA.printFnX(self,x)

bx = ClassB()
bx.printFnX(3)

该用例被描述为实现开放-封闭的方法“子类化的艺术”中的原则位于 http://www.youtube.com/watch?v=yrboy25WKGo&noredirect=1

Congratulations, you've discovered the motivating use case for Python's double-underscore name mangling :-)

For the details and a worked-out example see: http://docs.python.org/tutorial/classes.html#private-variables and at http://docs.python.org/reference/expressions.html#atom-identifiers .

Here's how to use it for your example:

# Base class definition
class ClassA(object):
    def __init__(self):
        print("Initializing A")

    # hoping that this function is called by this class's printFnX
    def fnX(self, x):
        return x**2
    __fnX = fnX

    def printFnX(self, x):
        print("ClassA:",self.__fnX(x))

# Inherits from ClassA above
class ClassB(ClassA):
    def __init__(self):
        print("initizlizing B")

    def fnX(self, x):
        return 2*x

    def printFnX(self, x):
        print("ClassB:", self.fnX(x))
        ClassA.printFnX(self,x)

bx = ClassB()
bx.printFnX(3)

The use case is described as a way of implementing the Open-Closed Principle in "The Art of Subclassing" found at http://www.youtube.com/watch?v=yrboy25WKGo&noredirect=1 .

未蓝澄海的烟 2024-12-17 23:18:25

通过将 fnXprintFnX 都设为类方法可以实现相同的效果。

class ClassA(object):

    def __init__(self):
        print("Initializing A")

    # hoping that this function is called by this class's printFnX
    @classmethod
    def fnX(self, x):
        return x ** 2

    @classmethod
    def printFnX(self, x):
        print("ClassA:",self.fnX(x))

class ClassB(ClassA):
    def __init__(self):
        print("initizlizing B")

    def fnX(self, x):
        return 2*x

    def printFnX(self, x):
        print("ClassB:", self.fnX(x))
        ClassA.printFnX(x)


bx = ClassB()<br>
bx.printFnX(3)

The same can be achieved by making fnX and printFnX both classmethods.

class ClassA(object):

    def __init__(self):
        print("Initializing A")

    # hoping that this function is called by this class's printFnX
    @classmethod
    def fnX(self, x):
        return x ** 2

    @classmethod
    def printFnX(self, x):
        print("ClassA:",self.fnX(x))

class ClassB(ClassA):
    def __init__(self):
        print("initizlizing B")

    def fnX(self, x):
        return 2*x

    def printFnX(self, x):
        print("ClassB:", self.fnX(x))
        ClassA.printFnX(x)


bx = ClassB()<br>
bx.printFnX(3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文