{"id":238021,"date":"2022-03-31T10:06:56","date_gmt":"2022-03-31T02:06:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=238021"},"modified":"2022-03-21T11:08:08","modified_gmt":"2022-03-21T03:08:08","slug":"about-c-inheritance","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/about-c-inheritance.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u7ee7\u627f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\u3002\u7ee7\u627f\u5141\u8bb8\u6211\u4eec\u6839\u636e\u4e00\u4e2a\u7c7b\u6765\u5b9a\u4e49\u53e6\u4e00\u4e2a\u7c7b\uff0c\u8fd9\u4f7f\u5f97\u521b\u5efa\u548c\u7ef4\u62a4\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u66f4\u5bb9\u6613\u3002\u540c\u65f6\u4e5f\u6709\u5229\u4e8e\u91cd\u7528\u4ee3\u7801\u548c\u8282\u7701\u5f00\u53d1\u65f6\u95f4\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5f53\u521b\u5efa\u4e00\u4e2a\u7c7b\u65f6\uff0c\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u5b8c\u5168\u91cd\u65b0\u7f16\u5199\u65b0\u7684\u6570\u636e\u6210\u5458\u548c\u6210\u5458\u51fd\u6570\uff0c\u53ea\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u65b0\u7684\u7c7b\uff0c\u7ee7\u627f\u4e86\u5df2\u6709\u7684\u7c7b\u7684\u6210\u5458\u5373\u53ef\u3002\u8fd9\u4e2a\u5df2\u6709\u7684\u7c7b\u88ab\u79f0\u4e3a\u7684\u57fa\u7c7b\uff0c\u8fd9\u4e2a\u65b0\u7684\u7c7b\u88ab\u79f0\u4e3a\u6d3e\u751f\u7c7b\u3002<\/p>\n

\u7ee7\u627f\u7684\u601d\u60f3\u5b9e\u73b0\u4e86 \u5c5e\u4e8e\uff08IS-A\uff09 \u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u54fa\u4e73\u52a8\u7269 \u5c5e\u4e8e\uff08IS-A\uff09 \u52a8\u7269\uff0c\u72d7 \u5c5e\u4e8e\uff08IS-A\uff09 \u54fa\u4e73\u52a8\u7269\uff0c\u56e0\u6b64\u72d7 \u5c5e\u4e8e\uff08IS-A\uff09 \u52a8\u7269\u3002<\/p>\n

\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b<\/strong><\/div>\n

\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u6d3e\u751f\u81ea\u591a\u4e2a\u7c7b\u6216\u63a5\u53e3\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u53ef\u4ee5\u4ece\u591a\u4e2a\u57fa\u7c7b\u6216\u63a5\u53e3\u7ee7\u627f\u6570\u636e\u548c\u51fd\u6570\u3002<\/p>\n

C# \u4e2d\u521b\u5efa\u6d3e\u751f\u7c7b\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

<\u8bbf\u95ee\u4fee\u9970\u7b26> class <\u57fa\u7c7b>\r\n{\r\n ...\r\n}\r\nclass <\u6d3e\u751f\u7c7b> : <\u57fa\u7c7b>\r\n{\r\n ...\r\n}<\/pre>\n

\u5047\u8bbe\uff0c\u6709\u4e00\u4e2a\u57fa\u7c7b Shape\uff0c\u5b83\u7684\u6d3e\u751f\u7c7b\u662f Rectangle\uff1a<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

using System;\r\nnamespace InheritanceApplication\r\n{\r\n   class Shape\r\n   {\r\n      public void setWidth(int w)\r\n      {\r\n         width = w;\r\n      }\r\n      public void setHeight(int h)\r\n      {\r\n         height = h;\r\n      }\r\n      protected int width;\r\n      protected int height;\r\n   }\r\n\r\n   \/\/ \u6d3e\u751f\u7c7b\r\n   class Rectangle: Shape\r\n   {\r\n      public int getArea()\r\n      {\r\n         return (width * height);\r\n      }\r\n   }\r\n   \r\n   class RectangleTester\r\n   {\r\n      static void Main(string[] args)\r\n      {\r\n         Rectangle Rect = new Rectangle();\r\n\r\n         Rect.setWidth(5);\r\n         Rect.setHeight(7);\r\n\r\n         \/\/ \u6253\u5370\u5bf9\u8c61\u7684\u9762\u79ef\r\n         Console.WriteLine(\"\u603b\u9762\u79ef\uff1a {0}\",  Rect.getArea());\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

\u603b\u9762\u79ef\uff1a 35<\/pre>\n
\u57fa\u7c7b\u7684\u521d\u59cb\u5316<\/strong><\/div>\n

\u6d3e\u751f\u7c7b\u7ee7\u627f\u4e86\u57fa\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u3002\u56e0\u6b64\u7236\u7c7b\u5bf9\u8c61\u5e94\u5728\u5b50\u7c7b\u5bf9\u8c61\u521b\u5efa\u4e4b\u524d\u88ab\u521b\u5efa\u3002\u60a8\u53ef\u4ee5\u5728\u6210\u5458\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8fdb\u884c\u7236\u7c7b\u7684\u521d\u59cb\u5316\u3002<\/p>\n

\u4e0b\u9762\u7684\u7a0b\u5e8f\u6f14\u793a\u4e86\u8fd9\u70b9\uff1a<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

using System;\r\nnamespace RectangleApplication\r\n{\r\n   class Rectangle\r\n   {\r\n      \/\/ \u6210\u5458\u53d8\u91cf\r\n      protected double length;\r\n      protected double width;\r\n      public Rectangle(double l, double w)\r\n      {\r\n         length = l;\r\n         width = w;\r\n      }\r\n      public double GetArea()\r\n      {\r\n         return length * width;\r\n      }\r\n      public void Display()\r\n      {\r\n         Console.WriteLine(\"\u957f\u5ea6\uff1a {0}\", length);\r\n         Console.WriteLine(\"\u5bbd\u5ea6\uff1a {0}\", width);\r\n         Console.WriteLine(\"\u9762\u79ef\uff1a {0}\", GetArea());\r\n      }\r\n   }\/\/end class Rectangle  \r\n   class Tabletop : Rectangle\r\n   {\r\n      private double cost;\r\n      public Tabletop(double l, double w) : base(l, w)\r\n      { }\r\n      public double GetCost()\r\n      {\r\n         double cost;\r\n         cost = GetArea() * 70;\r\n         return cost;\r\n      }\r\n      public void Display()\r\n      {\r\n         base.Display();\r\n         Console.WriteLine(\"\u6210\u672c\uff1a {0}\", GetCost());\r\n      }\r\n   }\r\n   class ExecuteRectangle\r\n   {\r\n      static void Main(string[] args)\r\n      {\r\n         Tabletop t = new Tabletop(4.5, 7.5);\r\n         t.Display();\r\n         Console.ReadLine();\r\n      }\r\n   }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

\u957f\u5ea6\uff1a 4.5\r\n\u5bbd\u5ea6\uff1a 7.5\r\n\u9762\u79ef\uff1a 33.75\r\n\u6210\u672c\uff1a 2362.5<\/pre>\n
C# \u591a\u91cd\u7ee7\u627f<\/strong><\/div>\n

\u591a\u91cd\u7ee7\u627f\u6307\u7684\u662f\u4e00\u4e2a\u7c7b\u522b\u53ef\u4ee5\u540c\u65f6\u4ece\u591a\u4e8e\u4e00\u4e2a\u7236\u7c7b\u7ee7\u627f\u884c\u4e3a\u4e0e\u7279\u5f81\u7684\u529f\u80fd\u3002\u4e0e\u5355\u4e00\u7ee7\u627f\u76f8\u5bf9\uff0c\u5355\u4e00\u7ee7\u627f\u6307\u4e00\u4e2a\u7c7b\u522b\u53ea\u53ef\u4ee5\u7ee7\u627f\u81ea\u4e00\u4e2a\u7236\u7c7b\u3002<\/p>\n

C# \u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u63a5\u53e3\u6765\u5b9e\u73b0\u591a\u91cd\u7ee7\u627f\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u6f14\u793a\u4e86\u8fd9\u70b9\uff1a<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

using System;\r\nnamespace InheritanceApplication\r\n{\r\n   class Shape\r\n   {\r\n      public void setWidth(int w)\r\n      {\r\n         width = w;\r\n      }\r\n      public void setHeight(int h)\r\n      {\r\n         height = h;\r\n      }\r\n      protected int width;\r\n      protected int height;\r\n   }\r\n\r\n   \/\/ \u57fa\u7c7b PaintCost\r\n   public interface PaintCost\r\n   {\r\n      int getCost(int area);\r\n\r\n   }\r\n   \/\/ \u6d3e\u751f\u7c7b\r\n   class Rectangle : Shape, PaintCost\r\n   {\r\n      public int getArea()\r\n      {\r\n         return (width * height);\r\n      }\r\n      public int getCost(int area)\r\n      {\r\n         return area * 70;\r\n      }\r\n   }\r\n   class RectangleTester\r\n   {\r\n      static void Main(string[] args)\r\n      {\r\n         Rectangle Rect = new Rectangle();\r\n         int area;\r\n         Rect.setWidth(5);\r\n         Rect.setHeight(7);\r\n         area = Rect.getArea();\r\n         \/\/ \u6253\u5370\u5bf9\u8c61\u7684\u9762\u79ef\r\n         Console.WriteLine(\"\u603b\u9762\u79ef\uff1a {0}\",  Rect.getArea());\r\n         Console.WriteLine(\"\u6cb9\u6f06\u603b\u6210\u672c\uff1a ${0}\" , Rect.getCost(area));\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

\u603b\u9762\u79ef\uff1a 35\r\n\u6cb9\u6f06\u603b\u6210\u672c\uff1a $2450<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5f53\u521b\u5efa\u4e00\u4e2a\u7c7b\u65f6\uff0c\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u5b8c\u5168\u91cd\u65b0\u7f16\u5199\u65b0\u7684\u6570\u636e\u6210\u5458\u548c\u6210\u5458\u51fd\u6570\uff0c\u53ea\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u65b0\u7684\u7c7b\uff0c\u7ee7\u627f\u4e86\u5df2\u6709\u7684\u7c7b\u7684\u6210\u5458\u5373\u53ef […]<\/p>\n","protected":false},"author":321,"featured_media":238023,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-238021","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=238021"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238021\/revisions"}],"predecessor-version":[{"id":238025,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/238021\/revisions\/238025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/238023"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=238021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=238021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=238021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}