python 实现类重载(类似C#的类重载,但是远不如C#的类重载好用)

pip install multipledispatch -i https://mirrors.aliyun.com/pypi/simple
from multipledispatch import dispatch

class MyClass:
    ab = 11234
    def __init__(self):
        self.aa = 1
    @dispatch()
    def calculate(self):
        self.aa = 1111

    @dispatch(int)
    def calculate(self, a):
        self._calculate(a)

    @classmethod
    def _calculate(cls, ip):
        print(cls.ab)
        print(ip)

obj = MyClass()
obj.calculate()
obj.calculate(5)
print(obj.aa)

 不多废话,直接看代码就行了,不知道百度一下就行了。

posted @ 2024-05-10 18:20  小天狼鑫  阅读(7)  评论(0编辑  收藏  举报