{"id":621,"date":"2023-12-26T09:01:01","date_gmt":"2023-12-26T01:01:01","guid":{"rendered":""},"modified":"2023-12-26T09:01:01","modified_gmt":"2023-12-26T01:01:01","slug":"C#\u91cd\u8f7d\u548c\u91cd\u5199","status":"publish","type":"post","link":"https:\/\/mushiming.com\/621.html","title":{"rendered":"C#\u91cd\u8f7d\u548c\u91cd\u5199"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4ec0\u4e48\u662f\u91cd\u5199\uff08override\uff09\uff1f<\/p>\n

\u201c\u91cd\u5199\u201d\u662f\u6307\u201c\u8986\u76d6\u5728\u67d0\u7269\u4e4b\u4e0a\u201d=\u201c\u8986\u76d6\u201d\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0e\u7ee7\u627f\u5bc6\u5207\u76f8\u5173\u7684\u529f\u80fd\u3002<\/p>\n

\u91cd\u5199\u5e94\u91cd\u65b0\u5b9a\u4e49\u7236\u7c7b\u65b9\u6cd5\u3002\u5f53\u60a8\u8981\u66f4\u6539\u7ee7\u627f\u7684\u7c7b\u4e2d\u7236\u65b9\u6cd5\u7684\u5904\u7406\u65f6\u4f7f\u7528\u3002<\/p>\n

public class Sample\n{\n  public virtual void Test()\n  {\n    \/\/ \u5904\u7406A\n  }\n}\n\nclass Sample2 : Sample\n{\n  public override void  Test()\n  {\n    \/\/ \u5904\u7406B\n  }\n}\n<\/code><\/pre>\n

\u5728\u4e0a\u8ff0\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8eSample2\u7c7b\u7ee7\u627f\u4e86Sample\u7c7b\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u8bbf\u95eeSample\u7c7b\u7684Test\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u5177\u6709\u8fd9\u79cd\u5173\u7cfb\u7684\u7c7b\u4e2d\u58f0\u660e\u4e0e\u8986\u76d6\u76f8\u540c\u7c7b\u578b\u548c\u540d\u79f0\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8986\u76d6\u5904\u7406\u5185\u5bb9\u3002\u5f53\u8fdb\u7a0b\u88ab\u8986\u76d6\u65f6\uff0c\u5728Sample2\u7c7b\u4e2d\u8c03\u7528Test\u65b9\u6cd5\u65f6\uff0c\u5c06\u6267\u884c\u5904\u7406B\u800c\u4e0d\u662f\u5904\u7406A\u3002
\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u5b9e\u4f8b\uff1a<\/p>\n

public class Sample\n{\n    public virtual void Test()\n    {\n        Debug.Log (\"111\");\n    }\n}\n\npublic class Sample2 : Sample\n{\n    public override void Test()\n    {\n        base.Test();\n        Debug.Log (\"222\");\n    }\n}\n<\/code><\/pre>\n

\u8fd9\u65f6\u51fa\u529b\u7684\u7ed3\u679c\u4e3a\u201c222\u201d\u800c\u4e0d\u662f\u201c111\u201d\u3002\u8fd9\u6837\uff0c\u91cd\u5199\u53ef\u4ee5\u91cd\u65b0\u5b9a\u4e49\u60a8\u4e0d\u60f3\u7ee7\u627f\u7684\u65b9\u6cd5\u6216\u8981\u5728\u7ee7\u627f\u7684\u7c7b\u4e2d\u66f4\u6539\u7684\u65b9\u6cd5\u3002\u91cd\u5199\u7684\u672c\u610f\u662f\u201c\u5efa\u7acb\u7236\u5b50\u5173\u7cfb\u201d\u3002<\/p>\n


\n

\u4ec0\u4e48\u662f\u91cd\u8f7d(Overload)\uff1f
\u91cd\u8f7d\u610f\u5473\u7740\u201c\u8d85\u8f7d\u201d\u3002\u6b64\u65b9\u6cd5\u5141\u8bb8\u60a8\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u5b9a\u4e49\u591a\u4e2a\u6210\u5458\u65b9\u6cd5\u3002\u4e0e\u91cd\u5199\u4e0d\u540c\uff0c\u5176\u4e2d\u540c\u4e00\u4e8b\u7269\uff08\u7ed3\u6784\u4e0d\u540c\u4f46\u540d\u79f0\u76f8\u540c\u7684\u4e8b\u7269\uff09\u5b58\u5728\u4e8e\u540c\u4e00\u57fa\u7840\u4e0a\u3002
\u91cd\u8f7d\u662f\u91cd\u8f7d\u540c\u540d\u7684\u65b9\u6cd5\u548c\u8fd0\u7b97\u7b26\u3002\u5177\u6709\u76f8\u540c\u540d\u79f0\u4f46\u53c2\u6570\u8f93\u5165\u6a21\u5f0f\u591a\u79cd\u7684\u65b9\u6cd5\uff0c<\/p>\n

\u5e76\u4e14\u53ef\u4ee5\u6267\u884c\u7b97\u672f\u8fd0\u7b97\u548c\u4f7f\u7528\u201c +\u201d\u8fdb\u884c\u5b57\u7b26\u8fde\u63a5\u3002<\/p>\n

class Sample\n{\n  void Test(int A)\n  {\n    \/\/ \u5904\u7406A\n  }\n\n  void Test (string B)\n  {\n    \/\/ \u5904\u7406B\n  }\n}\n<\/code><\/pre>\n

\u5728\u4e0a\u8ff0\u60c5\u51b5\u4e0b\uff0c\u540c\u4e00\u7c7b\u4e2d\u6709\u4e24\u4e2aTest\u65b9\u6cd5\u3002\u901a\u5e38\uff0c\u60a8\u4e0d\u80fd\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u5b9a\u4e49\u65b9\u6cd5\uff0c\u4f46\u662f\u5982\u679c\u7c7b\u578b\u548c\u53c2\u6570\u4e0d\u540c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u58f0\u660e\u65b9\u6cd5\u3002
\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e24\u4e2a\u65b9\u6cd5\u90fd\u5b58\u5728\u5e76\u4e14\u53ef\u4ee5\u88ab\u8c03\u7528\u3002\uff08\u91cd\u8f7d\uff09<\/p>\n


\n

\u603b\u7ed3\uff1a
\u2022\u91cd\u5199= \u91cd\u65b0\u5b9a\u4e49
\u2022\u91cd\u8f7d = \u8d85\u8f7d<\/p>\n","protected":false},"excerpt":{"rendered":"C#\u91cd\u8f7d\u548c\u91cd\u5199\u4ec0\u4e48\u662f\u91cd\u5199\uff08override\uff09\uff1f\u201c\u91cd\u5199\u201d\u662f\u6307\u201c\u8986\u76d6\u5728\u67d0\u7269\u4e4b\u4e0a\u201d=\u201c\u8986\u76d6\u201d\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0e\u7ee7\u627f\u5bc6\u5207\u76f8\u5173\u7684\u529f\u80fd\u3002\u91cd\u5199\u5e94\u91cd\u65b0\u5b9a\u4e49\u7236...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/621"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}