{"id":69,"date":"2021-11-25T19:20:29","date_gmt":"2021-11-25T11:20:29","guid":{"rendered":"https:\/\/www.helloalong.xyz\/?p=69"},"modified":"2021-11-25T19:20:29","modified_gmt":"2021-11-25T11:20:29","slug":"c-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.alonya.cn\/?p=69","title":{"rendered":"C# \u9762\u5411\u5bf9\u8c61"},"content":{"rendered":"<ol>\n<li>\n<p>\u5bf9\u8c61\u662f\u4e00\u7ec4\u5c5e\u6027\u548c\u65b9\u6cd5(\u4e8b\u4ef6)\u7684\u7ec4\u5408<\/p>\n<pre><code class=\"language-c#\">class program\n{\n   void main(string[] args)\n   {\n       \/\/\u6ca1\u6709\u7c7b\u7684\u8bdd\n       string userName = \"Tim\";\/\/\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf\n       int userAge = 18;\/\/\u5b9a\u4e49\u4e00\u4e2a32\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u53d8\u91cf\n       Console.WriteLine(\"Name:{0},Age{1}\",userName,userAge);\/\/\u6253\u5370\u7528\u6237\u4fe1\u606f\n\n       userName = \"Tom\";\/\/\u4fee\u6539\u7528\u6237\u4fe1\u606f\n       userAge = 20;\n       Console.WriteLine(\"Name:{0},Age{1}\",userName,userAge);\/\/\u6253\u5370\u7528\u6237\u4fe1\u606f\n       \/\/\u4e0a\u9762\u7684\u4ee3\u7801\u505a\u4e86\u4e24\u6b21\u76f8\u540c\u7684\u5de5\u4f5c\uff0c\u9664\u4e86\u53d8\u91cf\u503c\u4e0d\u540c\n       \/\/\u57fa\u4e8e\u9762\u5411\u5bf9\u8c61\u4e2d\u7684\"\u5c01\u88c5\"\u601d\u60f3\uff0c\u6211\u4eec\u5c06\"userName\",\"userAge\"\u548c\"\u8f93\u51fa\u8bed\u53e5\"\u6574\u5408\u5230\u4e00\u8d77\uff0c\u79f0\u4e3aclass\n       \/\/\u5c01\u88c5\u540e\u7684\u7c7b\uff0c\u53ea\u662f\u4e00\u4e2a\u6a21\u677f\uff0c\u9700\u8981\u5b9e\u4f8b\u5316\u624d\u80fd\u79f0\u4e3a\u4e00\u4e2a\u53ef\u64cd\u4f5c\u7684\u5bf9\u8c61,\u5c31\u50cf\u4e00\u5f20\u8868\uff0c\u89c4\u5b9a\u4e86\u5217\u540d\u4e4b\u540e\u9700\u8981\u81f3\u5c11\u4e00\u6761\u5b9e\u9645\u6570\u636e\u624d\u80fd\u4f53\u73b0\u5b83\u7684\u5185\u5bb9\n       \/\/\u7c7b\u7684\u5b9e\u4f8b\u5316,\u4e00\u822c\u540c\u65f6\u4e3a\u5b83\u7684\u4e00\u4e9b\u5c5e\u6027\u8d4b\u503c\n       User user1 = new User(\"Tim\",20);\n       \/\/\u8981\u64cd\u4f5c\u5bf9\u8c61\u5185\u90e8\u7684\u5c5e\u6027\u65b9\u6cd5\uff0c\u7528\u7c7b\u540d+\".\"+\u5c5e\u6027\/\u65b9\u6cd5\n       user1.ShowInfo();\n       \/\/\u5bf9\u8c61\u7684\u5c5e\u6027\u53ef\u4ee5\u968f\u65f6\u4fee\u6539\n       user1.name = \"Tom\";\n   }\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u7c7b\u7684\u4f5c\u7528\u5c31\u662f\u5b58\u50a8\u548c\u6267\u884c\u4e00\u7c7b\u5e38\u7528\u7684\u5c5e\u6027\u6216\u884c\u4e3a<\/p>\n<pre><code class=\"language-c#\">public class User\n{\n   public string name=\"\";\/\/\u5c5e\u6027\n   public int age = 0;\/\/\u5c5e\u6027\n   public User(string n,int a)\/\/\u6784\u9020\u65b9\u6cd5,\u4e0e\u65b9\u6cd5\u7c7b\u4f3c,\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\u81ea\u52a8\u8c03\u7528\n   {\n       this.name  = n;\n       this.age = a;\n   }\n   public void ShowInfo()\/\/\u65b9\u6cd5\n   {\n       Console.WriteLine(\"Name:{0},Age{1}\",name,age);\n   }\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u9762\u5411\u5bf9\u8c61\u7684\u4e09\u5927\u7279\u6027\uff08\u5c01\u88c5\/\u7ee7\u627f\/\u591a\u6001\uff09<\/p>\n<pre><code class=\"language-c#\">\/\/\u5c01\u88c5\u5c31\u662f\u7c7b\u7684\u58f0\u660e;\n\/\/\u7ee7\u627f\u5c31\u662f\u7528\u5df2\u6709\u7684\u7c7b\u4f5c\u4e3a\u6a21\u677f,\u76f4\u63a5\u62e5\u6709\u7236\u7c7b\u6709\u7684\u8d44\u6e90\npublic class MyUser:User{}\nMyUser myUser1 = new MyUser(\"Tim\",20);\nmyUser.ShowInfo();\n\n\/\/\u591a\u6001\u5728\u6a21\u677f\u7684\u57fa\u7840\u4e0a\u505a\u4e00\u4e9b\u5b9a\u5236\u5316\u7684\u4fee\u6539\npublic class VipUser:User\n{\n   public int Money = 99999999999999999999;\n   public void ShowInfo()\n   {\n       Console.WriteLine(\"Name:{0},Age{1},Money:{2}\",name,age,money);\n   }\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5bf9\u8c61\u80fd\u65e0\u9650\u5d4c\u5957<\/p>\n<pre><code class=\"language-c#\">public class Cat\n{\n   string name=\"cat\";\n   string voice = \"miao\";\n   void Speak()\n   {\n       Console.WriteLine(voice+\"!\");\n   }\n}\npublic class Human{}\n\npublic class Animal\n{\n   Cat cat = new Cat();\n   Human human = new Human();\n   \/\/...\n}\npublic class Plant{}\npublic class Germ{}\n\npublic cliass Creature\n{\n   Animal animal = new animal();\n   Plant plant = new plant();\n   Germ germ = new germ();\n}\n\/*-\u751f\u7269\n--\u52a8\u7269\n    ---\u732b\n        ----\u540d\u79f0\n        ----\u58f0\u97f3\n        ----\u53d1\u51fa\u53eb\u58f0()\n    ---\u4eba\u7c7b\n    ---...\n   --\u690d\u7269\n   --\u5fae\u751f\u7269<\/code><\/pre>\n<\/li>\n<li>\n<p>\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u7684\u7075\u9b42\u5728\u4e8e<code><code>.<\/code><\/code>(C#\/Java\/Python)<\/p>\n<pre><code class=\"language-c#\">Creature creature = new Creature();\ncreature.animal.cat.Speak();\n\/\/miao!<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u8c61\u662f\u4e00\u7ec4\u5c5e\u6027\u548c\u65b9\u6cd5(\u4e8b\u4ef6)\u7684\u7ec4\u5408 class program { void main(string[] ar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,19,10],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-net","category-19","category-development"],"_links":{"self":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=69"}],"version-history":[{"count":1,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions\/70"}],"wp:attachment":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}