{"id":3523,"date":"2025-04-13T13:48:49","date_gmt":"2025-04-13T05:48:49","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3523"},"modified":"2025-04-13T13:48:49","modified_gmt":"2025-04-13T05:48:49","slug":"c-%e5%a4%9a%e6%80%81%e6%80%a7%ef%bc%88polymorphism%ef%bc%89%e5%85%a8%e9%9d%a2%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e5%a4%9a%e6%80%81%e6%80%a7%ef%bc%88polymorphism%ef%bc%89%e5%85%a8%e9%9d%a2%e8%af%a6%e8%a7%a3\/","title":{"rendered":"C# \u591a\u6001\u6027\uff08Polymorphism\uff09\u5168\u9762\u8be6\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u662f\u5bf9 <strong>C# \u591a\u6001\u6027\uff08Polymorphism\uff09<\/strong> \u7684\u66f4\u5168\u9762\u3001\u7cfb\u7edf\u3001\u6df1\u5165\u7684\u89e3\u91ca\uff0c\u6db5\u76d6\u539f\u7406\u3001\u5206\u7c7b\u3001\u8bed\u6cd5\u673a\u5236\u3001\u5b9e\u6218\u5e94\u7528\u3001\u6ce8\u610f\u4e8b\u9879\u4ee5\u53ca\u6700\u4f73\u5b9e\u8df5\uff0c\u8f85\u4ee5\u9ad8\u8d28\u91cf\u5b98\u65b9\u6587\u6863\u548c\u793a\u4f8b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4ec0\u4e48\u662f\u591a\u6001\uff08Polymorphism\uff09\uff1f<\/h2>\n\n\n\n<p>\u201c\u591a\u6001\u201d\u6765\u81ea\u5e0c\u814a\u8bed\uff0c\u610f\u4e3a\u201c\u591a\u79cd\u5f62\u6001\u201d\u3002\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u5b83\u6307\u7684\u662f\u4f7f\u7528\u540c\u4e00\u4e2a\u65b9\u6cd5\u540d\u6216\u63a5\u53e3\uff0c\u5bf9\u4e0d\u540c\u7684\u5bf9\u8c61\u4ea7\u751f\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p>\ud83d\udccc \u901a\u4fd7\u7406\u89e3\uff1a\u540c\u4e00\u4e2a\u65b9\u6cd5\u8c03\u7528\uff0c\u5728\u4e0d\u540c\u7684\u7c7b\u4e0a\u6267\u884c\uff0c\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u591a\u6001\u7684\u4e24\u79cd\u5f62\u5f0f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u7279\u70b9<\/th><th>\u5b9e\u73b0\u65b9\u5f0f<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td>\u7f16\u8bd1\u65f6\u591a\u6001\uff08\u9759\u6001\uff09<\/td><td>\u7f16\u8bd1\u671f\u95f4\u786e\u5b9a\u8c03\u7528<\/td><td>\u65b9\u6cd5\u91cd\u8f7d\u3001\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/td><td><code>Add(int, int)<\/code> vs <code>Add(double, double)<\/code><\/td><\/tr><tr><td>\u8fd0\u884c\u65f6\u591a\u6001\uff08\u52a8\u6001\uff09<\/td><td>\u7a0b\u5e8f\u8fd0\u884c\u65f6\u51b3\u5b9a\u8c03\u7528\u54ea\u4e00\u4e2a\u5b9e\u73b0<\/td><td>\u865a\u65b9\u6cd5 + \u91cd\u5199\u3001\u63a5\u53e3\u5b9e\u73b0<\/td><td>\u7236\u7c7b\u53d8\u91cf\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\u8c03\u7528\u88ab\u91cd\u5199\u7684\u65b9\u6cd5<\/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\">3. \u7f16\u8bd1\u65f6\u591a\u6001\uff08\u9759\u6001\u591a\u6001\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u65b9\u6cd5\u91cd\u8f7d\uff08Method Overloading\uff09<\/h3>\n\n\n\n<p>\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u5141\u8bb8\u65b9\u6cd5\u540d\u76f8\u540c\uff0c\u4f46\u53c2\u6570\u7c7b\u578b\u3001\u4e2a\u6570\u3001\u987a\u5e8f\u4e0d\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Printer {\n    public void Print(string text) =&gt; Console.WriteLine(text);\n    public void Print(int number) =&gt; Console.WriteLine(number);\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u671f\u95f4\u6839\u636e\u4f20\u5165\u53c2\u6570\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u8fd0\u7b97\u7b26\u91cd\u8f7d\uff08Operator Overloading\uff09<\/h3>\n\n\n\n<p>\u53ef\u4ee5\u5b9a\u4e49\u5982\u4f55\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u578b\u4f7f\u7528 <code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code> \u7b49\u8fd0\u7b97\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vector {\n    public int X, Y;\n    public static Vector operator +(Vector v1, Vector v2)\n        =&gt; new Vector { X = v1.X + v2.X, Y = v1.Y + v2.Y };\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u8fd0\u884c\u65f6\u591a\u6001\uff08\u52a8\u6001\u591a\u6001\uff09<\/h2>\n\n\n\n<p>\u901a\u8fc7 <strong>\u865a\u65b9\u6cd5\uff08<code>virtual<\/code>\uff09<\/strong> \u548c <strong>\u65b9\u6cd5\u91cd\u5199\uff08<code>override<\/code>\uff09<\/strong> \u5b9e\u73b0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u4f7f\u7528\u865a\u65b9\u6cd5\u4e0e\u91cd\u5199<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    public virtual void Speak() =&gt; Console.WriteLine(\"Animal speaks\");\n}\n\nclass Dog : Animal {\n    public override void Speak() =&gt; Console.WriteLine(\"Dog barks\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 \u591a\u6001\u6027\u6f14\u793a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal myPet = new Dog();\nmyPet.Speak();  \/\/ \u8f93\u51fa: Dog barks<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \u5c3d\u7ba1\u53d8\u91cf\u662f <code>Animal<\/code> \u7c7b\u578b\uff0c\u4f46\u8c03\u7528\u7684\u662f <code>Dog<\/code> \u7c7b\u4e2d\u7684 <code>Speak<\/code> \u65b9\u6cd5\uff0c\u8fd9\u662f\u8fd0\u884c\u65f6\u591a\u6001\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u63a5\u53e3\u591a\u6001\uff08Interface-based Polymorphism\uff09<\/h2>\n\n\n\n<p>\u63a5\u53e3\u662f\u5b9e\u73b0\u591a\u6001\u6027\u7684\u53e6\u4e00\u79cd\u91cd\u8981\u65b9\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface IShape {\n    void Draw();\n}\n\nclass Circle : IShape {\n    public void Draw() =&gt; Console.WriteLine(\"Drawing Circle\");\n}\n\nclass Square : IShape {\n    public void Draw() =&gt; Console.WriteLine(\"Drawing Square\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u63a5\u53e3\u591a\u6001<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>IShape shape = new Circle();\nshape.Draw();  \/\/ \u8f93\u51fa Drawing Circle<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u62bd\u8c61\u7c7b\u4e0e\u591a\u6001\uff08Abstract Class\uff09<\/h2>\n\n\n\n<p>\u62bd\u8c61\u7c7b\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u62bd\u8c61\u65b9\u6cd5\uff0c\u8981\u6c42\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Shape {\n    public abstract void Draw();\n}\n\nclass Triangle : Shape {\n    public override void Draw() =&gt; Console.WriteLine(\"Draw triangle\");\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Shape s = new Triangle();\ns.Draw(); \/\/ \u8f93\u51fa Draw triangle<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u591a\u6001\u7684\u5e94\u7528\u573a\u666f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u591a\u6001\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td>GUI \u63a7\u4ef6\u7cfb\u7edf<\/td><td>\u6240\u6709\u63a7\u4ef6\u7ee7\u627f\u81ea\u540c\u4e00\u57fa\u7c7b\uff0c\u91cd\u5199\u7ed8\u5236\u884c\u4e3a<\/td><\/tr><tr><td>\u65e5\u5fd7\u7cfb\u7edf<\/td><td>\u62bd\u8c61\u65e5\u5fd7\u5199\u5165\uff0c\u652f\u6301\u5199\u5165\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u591a\u79cd\u65b9\u5f0f<\/td><\/tr><tr><td>\u6570\u636e\u8bbf\u95ee\u5c42<\/td><td>\u4f7f\u7528\u63a5\u53e3\u6765\u7edf\u4e00\u4e0d\u540c\u6570\u636e\u5e93\u7684\u64cd\u4f5c\uff08\u5982 MySQL\u3001SQL Server\uff09<\/td><\/tr><tr><td>\u6e38\u620f\u5f00\u53d1<\/td><td>\u654c\u4eba\u3001NPC \u90fd\u7ee7\u627f\u81ea Entity \u7c7b\uff0c\u8c03\u7528\u7edf\u4e00\u65b9\u6cd5\u5904\u7406\u884c\u4e3a<\/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\">8. \u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u70b9<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>new<\/code> \u9690\u85cf<\/td><td>\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u53ef\u4ee5\u9690\u85cf\u57fa\u7c7b\u65b9\u6cd5\uff0c\u4f46\u4e0d\u662f\u771f\u6b63\u7684\u91cd\u5199<\/td><\/tr><tr><td>\u975e\u865a\u65b9\u6cd5<\/td><td>\u4e0d\u80fd\u91cd\u5199\u672a\u6807\u8bb0\u4e3a <code>virtual<\/code> \u6216 <code>abstract<\/code> \u7684\u65b9\u6cd5<\/td><\/tr><tr><td>\u591a\u6001\u7ed1\u5b9a<\/td><td>\u53ea\u6709\u901a\u8fc7\u57fa\u7c7b\u6216\u63a5\u53e3\u5f15\u7528\u8c03\u7528\u65b9\u6cd5\u65f6\u624d\u4f53\u73b0\u51fa\u591a\u6001\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>public class A {\n    public virtual void Show() =&gt; Console.WriteLine(\"A\");\n}\n\npublic class B : A {\n    public new void Show() =&gt; Console.WriteLine(\"B\");\n}\n\nA obj = new B();\nobj.Show(); \/\/ \u8f93\u51fa A\uff08\u4e0d\u662f B\uff09<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. \u5b98\u65b9\u6587\u6863\u4e0e\u6743\u5a01\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/fundamentals\/object-oriented\/polymorphism\">Polymorphism &#8211; Microsoft Learn<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/keywords\/virtual\">Virtual and Override Keywords &#8211; Microsoft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/interfaces\/\">Interfaces &#8211; Microsoft C# Guide<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/keywords\/abstract\">Abstract Classes &#8211; Microsoft Docs<\/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\">10. \u591a\u6001\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b56\u7565\u6a21\u5f0f\uff08Strategy Pattern\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface ICompressionStrategy {\n    void Compress(string fileName);\n}\n\nclass ZipCompression : ICompressionStrategy {\n    public void Compress(string fileName) =&gt; Console.WriteLine(\"ZIP Compression\");\n}\n\nclass RarCompression : ICompressionStrategy {\n    public void Compress(string fileName) =&gt; Console.WriteLine(\"RAR Compression\");\n}\n\nclass Compressor {\n    private ICompressionStrategy _strategy;\n    public Compressor(ICompressionStrategy strategy) =&gt; _strategy = strategy;\n    public void Compress(string fileName) =&gt; _strategy.Compress(fileName);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var compressor = new Compressor(new ZipCompression());\ncompressor.Compress(\"file.txt\");  \/\/ \u8f93\u51fa: ZIP Compression<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5206\u7c7b<\/th><th>\u7f16\u8bd1\u65f6\u591a\u6001<\/th><th>\u8fd0\u884c\u65f6\u591a\u6001<\/th><\/tr><\/thead><tbody><tr><td>\u5b9e\u73b0\u65b9\u5f0f<\/td><td>\u91cd\u8f7d\/\u91cd\u8f7d\u8fd0\u7b97\u7b26<\/td><td>\u865a\u51fd\u6570 + \u91cd\u5199<\/td><\/tr><tr><td>\u5224\u65ad\u65f6\u95f4<\/td><td>\u7f16\u8bd1\u65f6<\/td><td>\u8fd0\u884c\u65f6<\/td><\/tr><tr><td>\u4f18\u70b9<\/td><td>\u7b80\u5355\u3001\u9ad8\u6548<\/td><td>\u7075\u6d3b\u3001\u53ef\u6269\u5c55<\/td><\/tr><tr><td>\u4ee3\u8868\u5173\u952e\u8bcd<\/td><td><code>overload<\/code>, <code>operator<\/code><\/td><td><code>virtual<\/code>, <code>override<\/code>, <code>interface<\/code>, <code>abstract<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u5bf9 C# \u591a\u6001\u6027\uff08Polymorphism\uff09 \u7684\u66f4\u5168\u9762\u3001\u7cfb\u7edf\u3001\u6df1\u5165\u7684\u89e3\u91ca\uff0c\u6db5\u76d6\u539f\u7406\u3001\u5206\u7c7b\u3001\u8bed\u6cd5\u673a\u5236\u3001\u5b9e\u6218 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3524,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3523","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=3523"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3523\/revisions"}],"predecessor-version":[{"id":3525,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3523\/revisions\/3525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3524"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}