{"id":3520,"date":"2025-04-13T13:41:30","date_gmt":"2025-04-13T05:41:30","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3520"},"modified":"2025-04-13T13:41:30","modified_gmt":"2025-04-13T05:41:30","slug":"c-%e7%bb%a7%e6%89%bf%ef%bc%88inheritance%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e7%bb%a7%e6%89%bf%ef%bc%88inheritance%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"C# \u7ee7\u627f\uff08Inheritance\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7c7b\u4ece\u53e6\u4e00\u4e2a\u7c7b\u7ee7\u627f\u6210\u5458\uff08\u5982\u5b57\u6bb5\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u7b49\uff09\u3002\u5728 C# \u4e2d\uff0c\u7ee7\u627f\u901a\u8fc7 <code>:<\/code> \u8bed\u6cd5\u6765\u5b9e\u73b0\u3002\u7ee7\u627f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u63d0\u9ad8\u4ee3\u7801\u7684\u91cd\u7528\u6027\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u591a\u6001\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\u7ee7\u627f\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u57fa\u7c7b\uff08Parent Class\uff09<\/strong>\uff1a\u88ab\u7ee7\u627f\u7684\u7c7b\u3002<\/li>\n\n\n\n<li><strong>\u5b50\u7c7b\uff08Derived Class\uff09<\/strong>\uff1a\u7ee7\u627f\u57fa\u7c7b\u7684\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u57fa\u7c7b\u7684\u529f\u80fd\uff0c\u4e14\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u529f\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ee7\u627f\u7684\u7279\u70b9\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u57fa\u7c7b\u7684\u6210\u5458<\/strong>\uff1a\u5b50\u7c7b\u53ef\u4ee5\u8bbf\u95ee\u57fa\u7c7b\u4e2d\u7684\u516c\u5171\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\u3002<\/li>\n\n\n\n<li><strong>\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u57fa\u7c7b\u7684\u65b9\u6cd5<\/strong>\uff1a\u5b50\u7c7b\u53ef\u4ee5\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u5b9e\u73b0\uff08\u65b9\u6cd5\u91cd\u5199\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5b50\u7c7b\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u6210\u5458<\/strong>\uff1a\u9664\u4e86\u7ee7\u627f\u7236\u7c7b\u7684\u6210\u5458\u5916\uff0c\u5b50\u7c7b\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u5b57\u6bb5\u3001\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u5355\u7ee7\u627f<\/strong>\uff1aC# \u53ea\u652f\u6301\u5355\u7ee7\u627f\uff0c\u5373\u4e00\u4e2a\u7c7b\u53ea\u80fd\u7ee7\u627f\u4e00\u4e2a\u7c7b\uff0c\u4f46\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u63a5\u53e3\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\">\u4e8c\u3001\u7ee7\u627f\u7684\u57fa\u672c\u8bed\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u57fa\u7c7b\uff08Parent Class\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    public void Eat()\n    {\n        Console.WriteLine(\"Animal is eating.\");\n    }\n\n    public void Sleep()\n    {\n        Console.WriteLine(\"Animal is sleeping.\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5b50\u7c7b\uff08Derived Class\uff09<\/h3>\n\n\n\n<p>\u901a\u8fc7 <code>:<\/code> \u5173\u952e\u5b57\uff0c\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u57fa\u7c7b\u7684\u6210\u5458\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog : Animal  \/\/ Dog \u7c7b\u7ee7\u627f\u81ea Animal \u7c7b\n{\n    public void Bark()\n    {\n        Console.WriteLine(\"Dog is barking.\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u521b\u5efa\u5b50\u7c7b\u5b9e\u4f8b\u5e76\u4f7f\u7528\u7ee7\u627f\u7684\u529f\u80fd<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog dog = new Dog();\ndog.Eat();    \/\/ \u7ee7\u627f\u81ea Animal \u7c7b\ndog.Sleep();  \/\/ \u7ee7\u627f\u81ea Animal \u7c7b\ndog.Bark();   \/\/ Dog \u7c7b\u81ea\u5df1\u7684\u65b9\u6cd5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u6784\u9020\u51fd\u6570\u548c\u7ee7\u627f<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u57fa\u7c7b\u6784\u9020\u51fd\u6570<\/strong>\uff1a\u5982\u679c\u57fa\u7c7b\u5b9a\u4e49\u4e86\u6784\u9020\u51fd\u6570\uff0c\u5b50\u7c7b\u9700\u8981\u4f7f\u7528 <code>base<\/code> \u5173\u952e\u5b57\u6765\u8c03\u7528\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u5b50\u7c7b\u6784\u9020\u51fd\u6570<\/strong>\uff1a\u5982\u679c\u5b50\u7c7b\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0cC# \u4f1a\u81ea\u52a8\u8c03\u7528\u65e0\u53c2\u7684\u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    public Animal(string name)\n    {\n        Console.WriteLine(\"Animal's name is \" + name);\n    }\n}\n\nclass Dog : Animal\n{\n    public Dog(string name) : base(name)  \/\/ \u8c03\u7528\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\n    {\n        Console.WriteLine(\"Dog is created.\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u5bf9\u8c61\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog dog = new Dog(\"Buddy\");<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal's name is Buddy\nDog is created.<\/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\u65b9\u6cd5\u91cd\u5199\u4e0e\u865a\u62df\u65b9\u6cd5<\/h2>\n\n\n\n<p>C# \u652f\u6301\u65b9\u6cd5\u91cd\u5199\uff08Overriding\uff09\uff0c\u5b50\u7c7b\u53ef\u4ee5\u91cd\u65b0\u5b9a\u4e49\u4ece\u57fa\u7c7b\u7ee7\u627f\u7684\u65b9\u6cd5\u3002\u65b9\u6cd5\u91cd\u5199\u4f7f\u7528 <code>virtual<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u5b50\u7c7b\u4f7f\u7528 <code>override<\/code> \u5173\u952e\u5b57\u91cd\u5199\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u57fa\u7c7b\u5b9a\u4e49\u865a\u62df\u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    public virtual void Speak()  \/\/ \u4f7f\u7528 virtual \u5173\u952e\u5b57\u5b9a\u4e49\u865a\u62df\u65b9\u6cd5\n    {\n        Console.WriteLine(\"Animal speaks\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5b50\u7c7b\u91cd\u5199\u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog : Animal\n{\n    public override void Speak()  \/\/ \u4f7f\u7528 override \u5173\u952e\u5b57\u91cd\u5199\u65b9\u6cd5\n    {\n        Console.WriteLine(\"Dog barks\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528\u91cd\u5199\u7684\u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal animal = new Dog();  \/\/ \u57fa\u7c7b\u5f15\u7528\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\nanimal.Speak();  \/\/ \u8f93\u51fa Dog barks<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u8bbf\u95ee\u4fee\u9970\u7b26\u548c\u7ee7\u627f<\/h2>\n\n\n\n<p>C# \u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u53ef\u4ee5\u5f71\u54cd\u7ee7\u627f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>private<\/code> \u6210\u5458\u4e0d\u80fd\u88ab\u7ee7\u627f\uff0c<code>protected<\/code> \u6210\u5458\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u516c\u5171\u6210\u5458\uff08<code>public<\/code>\uff09<\/h3>\n\n\n\n<p>\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u5e76\u8bbf\u95ee\u57fa\u7c7b\u7684\u516c\u5171\u6210\u5458\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    public string Name;\n}\n\nclass Dog : Animal\n{\n    public void PrintName()\n    {\n        Console.WriteLine(Name);  \/\/ \u7ee7\u627f\u5e76\u8bbf\u95ee\u57fa\u7c7b\u7684\u516c\u5171\u6210\u5458\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4fdd\u62a4\u6210\u5458\uff08<code>protected<\/code>\uff09<\/h3>\n\n\n\n<p>\u4fdd\u62a4\u6210\u5458\u53ea\u80fd\u5728\u7c7b\u53ca\u5176\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    protected string Name;\n}\n\nclass Dog : Animal\n{\n    public void SetName(string name)\n    {\n        Name = name;  \/\/ \u53ef\u4ee5\u8bbf\u95ee\u57fa\u7c7b\u7684\u4fdd\u62a4\u6210\u5458\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u79c1\u6709\u6210\u5458\uff08<code>private<\/code>\uff09<\/h3>\n\n\n\n<p>\u79c1\u6709\u6210\u5458\u65e0\u6cd5\u88ab\u5b50\u7c7b\u76f4\u63a5\u8bbf\u95ee\uff0c\u53ea\u80fd\u901a\u8fc7\u516c\u5171\u65b9\u6cd5\u6216\u5c5e\u6027\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    private string name;\n\n    public string GetName()  \/\/ \u901a\u8fc7\u516c\u5171\u65b9\u6cd5\u8bbf\u95ee\u79c1\u6709\u6210\u5458\n    {\n        return name;\n    }\n}\n\nclass Dog : Animal\n{\n    public void PrintName()\n    {\n        Console.WriteLine(GetName());  \/\/ \u8bbf\u95ee\u57fa\u7c7b\u7684\u79c1\u6709\u6210\u5458\n    }\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\">\u516d\u3001\u57fa\u7c7b\u4e0e\u5b50\u7c7b\u7684\u7c7b\u578b\u8f6c\u6362<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5411\u4e0a\u8f6c\u578b\uff08Upcasting\uff09<\/strong>\uff1a\u5b50\u7c7b\u5bf9\u8c61\u53ef\u4ee5\u8d4b\u7ed9\u7236\u7c7b\u5f15\u7528\u53d8\u91cf\u3002\u5411\u4e0a\u8f6c\u578b\u662f\u81ea\u52a8\u7684\uff0c\u56e0\u4e3a\u5b50\u7c7b\u662f\u7236\u7c7b\u7684\u7c7b\u578b\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Dog dog = new Dog();\n   Animal animal = dog;  \/\/ \u5411\u4e0a\u8f6c\u578b<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5411\u4e0b\u8f6c\u578b\uff08Downcasting\uff09<\/strong>\uff1a\u7236\u7c7b\u5f15\u7528\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5b50\u7c7b\u7c7b\u578b\uff0c\u4f46\u5fc5\u987b\u663e\u5f0f\u8f6c\u6362\uff0c\u5e76\u4e14\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Animal animal = new Dog();\n   Dog dog = (Dog)animal;  \/\/ \u5411\u4e0b\u8f6c\u578b\n\n   \/\/ \u63a8\u8350\u4f7f\u7528 `as` \u6216 `is` \u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\n   if (animal is Dog dogObj)\n   {\n       dogObj.Bark();\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\">\u4e03\u3001\u53c2\u8003\u6587\u732e\u548c\u6587\u6863<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/inheritance\">C# \u7ee7\u627f &#8211; Microsoft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/polymorphism\">C# \u7ee7\u627f\u4e0e\u591a\u6001 &#8211; Microsoft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/derived-classes\">C# \u57fa\u7c7b\u4e0e\u6d3e\u751f\u7c7b\u793a\u4f8b &#8211; Microsoft<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u7ee7\u627f\uff0cC# \u5141\u8bb8\u5f00\u53d1\u8005\u5b9e\u73b0\u66f4\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u4ee3\u7801\u91cd\u7528\uff0c\u5e76\u80fd\u591f\u66f4\u597d\u5730\u652f\u6301\u591a\u6001\u6027\uff0c\u4f7f\u5f97\u8f6f\u4ef6\u5f00\u53d1\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u6269\u5c55\u3002<\/p>\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>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7c7b\u4ece\u53e6\u4e00\u4e2a\u7c7b\u7ee7\u627f\u6210\u5458\uff08\u5982\u5b57\u6bb5\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u7b49\uff09\u3002\u5728 C# \u4e2d\uff0c\u7ee7\u627f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3520","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\/3520","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=3520"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3520\/revisions"}],"predecessor-version":[{"id":3522,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3520\/revisions\/3522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3521"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}