{"id":3759,"date":"2025-05-29T23:52:38","date_gmt":"2025-05-29T15:52:38","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3759"},"modified":"2025-05-29T23:52:38","modified_gmt":"2025-05-29T15:52:38","slug":"%e5%9c%a8-python-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%bf%90%e7%ae%97%e7%ac%a6%e9%87%8d%e8%bd%bd%ef%bc%88operator-overloading%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/05\/29\/%e5%9c%a8-python-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%bf%90%e7%ae%97%e7%ac%a6%e9%87%8d%e8%bd%bd%ef%bc%88operator-overloading%ef%bc%89\/","title":{"rendered":"\u5728 Python \u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff08Operator Overloading\uff09"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c**\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff08Operator Overloading\uff09**\u5141\u8bb8\u4f60\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u5b9a\u4e49\u8fd0\u7b97\u7b26\u7684\u884c\u4e3a\uff0c\u6bd4\u5982 <code>+<\/code>\u3001<code>-<\/code>\u3001<code>==<\/code>\u3001<code>&lt;<\/code> \u7b49\u3002\u5b83\u901a\u8fc7 <strong>\u7279\u6b8a\u65b9\u6cd5\uff08\u9b54\u672f\u65b9\u6cd5\uff09<\/strong> \u5b9e\u73b0\uff0c\u5982 <code>__add__()<\/code>\u3001<code>__eq__()<\/code> \u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u672c\u793a\u4f8b\uff1a\u91cd\u8f7d <code>+<\/code> \u8fd0\u7b97\u7b26<\/h2>\n\n\n\n<p>\u4ee5\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u4e8c\u7ef4\u5411\u91cf\u7c7b <code>Vector2D<\/code> \u4e3a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vector2D:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    # \u91cd\u8f7d + \u8fd0\u7b97\u7b26\n    def __add__(self, other):\n        return Vector2D(self.x + other.x, self.y + other.y)\n\n    def __repr__(self):\n        return f\"Vector2D({self.x}, {self.y})\"\n\n# \u6d4b\u8bd5\nv1 = Vector2D(1, 2)\nv2 = Vector2D(3, 4)\nprint(v1 + v2)  # \u8f93\u51fa\uff1aVector2D(4, 6)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5e38\u7528\u8fd0\u7b97\u7b26\u53ca\u5176\u9b54\u672f\u65b9\u6cd5\u5bf9\u7167\u8868<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fd0\u7b97\u7b26<\/th><th>\u9b54\u672f\u65b9\u6cd5<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td><code>__add__(self, other)<\/code><\/td><td>\u52a0\u6cd5<\/td><\/tr><tr><td><code>-<\/code><\/td><td><code>__sub__<\/code><\/td><td>\u51cf\u6cd5<\/td><\/tr><tr><td><code>*<\/code><\/td><td><code>__mul__<\/code><\/td><td>\u4e58\u6cd5<\/td><\/tr><tr><td><code>\/<\/code><\/td><td><code>__truediv__<\/code><\/td><td>\u9664\u6cd5\uff08\u771f\u9664\u6cd5\uff09<\/td><\/tr><tr><td><code>\/\/<\/code><\/td><td><code>__floordiv__<\/code><\/td><td>\u5730\u677f\u9664<\/td><\/tr><tr><td><code>%<\/code><\/td><td><code>__mod__<\/code><\/td><td>\u53d6\u6a21<\/td><\/tr><tr><td><code>**<\/code><\/td><td><code>__pow__<\/code><\/td><td>\u5e42\u8fd0\u7b97<\/td><\/tr><tr><td><code>==<\/code><\/td><td><code>__eq__<\/code><\/td><td>\u7b49\u4e8e<\/td><\/tr><tr><td><code>!=<\/code><\/td><td><code>__ne__<\/code><\/td><td>\u4e0d\u7b49\u4e8e<\/td><\/tr><tr><td><code>&lt;<\/code><\/td><td><code>__lt__<\/code><\/td><td>\u5c0f\u4e8e<\/td><\/tr><tr><td><code>&lt;=<\/code><\/td><td><code>__le__<\/code><\/td><td>\u5c0f\u4e8e\u7b49\u4e8e<\/td><\/tr><tr><td><code>&gt;<\/code><\/td><td><code>__gt__<\/code><\/td><td>\u5927\u4e8e<\/td><\/tr><tr><td><code>&gt;=<\/code><\/td><td><code>__ge__<\/code><\/td><td>\u5927\u4e8e\u7b49\u4e8e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u8fdb\u4e00\u6b65\u793a\u4f8b\uff1a\u91cd\u8f7d <code>==<\/code> \u548c <code>&lt;<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student:\n    def __init__(self, name, score):\n        self.name = name\n        self.score = score\n\n    def __eq__(self, other):\n        return self.score == other.score\n\n    def __lt__(self, other):\n        return self.score &lt; other.score\n\n# \u6d4b\u8bd5\ns1 = Student(\"Alice\", 90)\ns2 = Student(\"Bob\", 85)\nprint(s1 == s2)  # False\nprint(s1 &lt; s2)   # False<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7c7b\u578b\u68c0\u67e5\u662f\u4e2a\u597d\u4e60\u60ef<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   def __add__(self, other):\n       if isinstance(other, Vector2D):\n           return Vector2D(self.x + other.x, self.y + other.y)\n       return NotImplemented<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5e94\u59cb\u7ec8\u5b9e\u73b0\u6210\u5bf9\u7684\u65b9\u6cd5<\/strong>\uff08\u5982\u5b9e\u73b0\u4e86 <code>__eq__<\/code>\uff0c\u6700\u597d\u4e5f\u5b9e\u73b0 <code>__ne__<\/code>\uff09<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u6ee5\u7528\u91cd\u8f7d<\/strong>\uff1a\u907f\u514d\u8fc7\u5ea6\u91cd\u8f7d\u9020\u6210\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u51fa\u7ad9\u53c2\u8003\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udcd8 Python \u5b98\u65b9\u6587\u6863\uff1a<br>\ud83d\udd17 <a href=\"https:\/\/docs.python.org\/3\/reference\/datamodel.html#special-method-names\">https:\/\/docs.python.org\/3\/reference\/datamodel.html#special-method-names<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u7075\u6d3b\u6027\u9ad8<\/td><td>\u53ef\u4e3a\u7c7b\u81ea\u5b9a\u4e49\u8fd0\u7b97\u884c\u4e3a<\/td><\/tr><tr><td>\u4f7f\u7528\u9b54\u672f\u65b9\u6cd5<\/td><td>\u5982 <code>__add__<\/code>, <code>__eq__<\/code><\/td><\/tr><tr><td>\u7528\u4e8e\u81ea\u5b9a\u4e49\u7c7b<\/td><td>\u9002\u5408 <code>\u5411\u91cf\/\u77e9\u9635\/\u590d\u6570\/\u6a21\u578b\u5bf9\u8c61<\/code><\/td><\/tr><tr><td>\u63a8\u8350\u642d\u914d\u4f7f\u7528<\/td><td><code>__str__<\/code>\u3001<code>__repr__<\/code> \u66f4\u6613\u8c03\u8bd5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c**\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff08Operator Overloading\uff09**\u5141\u8bb8\u4f60\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u5b9a\u4e49\u8fd0\u7b97\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3760,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-3759","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3759"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3759\/revisions"}],"predecessor-version":[{"id":3761,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3759\/revisions\/3761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3760"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}