{"id":245892,"date":"2022-07-06T09:17:49","date_gmt":"2022-07-06T01:17:49","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=245892"},"modified":"2022-06-23T18:18:17","modified_gmt":"2022-06-23T10:18:17","slug":"about-c-properties","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/about-c-properties.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u5c5e\u6027\uff08Property\uff09"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5c5e\u6027\uff08Property\uff09 \u662f\u7c7b\uff08class\uff09\u3001\u7ed3\u6784\uff08structure\uff09\u548c\u63a5\u53e3\uff08interface\uff09\u7684\u547d\u540d\uff08named\uff09\u6210\u5458\u3002\u7c7b\u6216\u7ed3\u6784\u4e2d\u7684\u6210\u5458\u53d8\u91cf\u6216\u65b9\u6cd5\u79f0\u4e3a \u57df\uff08Field\uff09\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5c5e\u6027\uff08Property\uff09\u662f\u57df\uff08Field\uff09\u7684\u6269\u5c55\uff0c\u4e14\u53ef\u4f7f\u7528\u76f8\u540c\u7684\u8bed\u6cd5\u6765\u8bbf\u95ee\u3002\u5b83\u4eec\u4f7f\u7528 \u8bbf\u95ee\u5668\uff08accessors\uff09 \u8ba9\u79c1\u6709\u57df\u7684\u503c\u53ef\u88ab\u8bfb\u5199\u6216\u64cd\u4f5c\u3002<\/p>\n

\u5c5e\u6027\uff08Property\uff09\u4e0d\u4f1a\u786e\u5b9a\u5b58\u50a8\u4f4d\u7f6e\u3002\u76f8\u53cd\uff0c\u5b83\u4eec\u5177\u6709\u53ef\u8bfb\u5199\u6216\u8ba1\u7b97\u5b83\u4eec\u503c\u7684 \u8bbf\u95ee\u5668\uff08accessors\uff09\u3002<\/p>\n

\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u540d\u4e3a Student \u7684\u7c7b\uff0c\u5e26\u6709 age\u3001name \u548c code \u7684\u79c1\u6709\u57df\u3002\u6211\u4eec\u4e0d\u80fd\u5728\u7c7b\u7684\u8303\u56f4\u4ee5\u5916\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u57df\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u62e5\u6709\u8bbf\u95ee\u8fd9\u4e9b\u79c1\u6709\u57df\u7684\u5c5e\u6027\u3002<\/p>\n

\u8bbf\u95ee\u5668\uff08Accessors\uff09<\/strong><\/div>\n

\u5c5e\u6027\uff08Property\uff09\u7684\u8bbf\u95ee\u5668\uff08accessor\uff09\u5305\u542b\u6709\u52a9\u4e8e\u83b7\u53d6\uff08\u8bfb\u53d6\u6216\u8ba1\u7b97\uff09\u6216\u8bbe\u7f6e\uff08\u5199\u5165\uff09\u5c5e\u6027\u7684\u53ef\u6267\u884c\u8bed\u53e5\u3002\u8bbf\u95ee\u5668\uff08accessor\uff09\u58f0\u660e\u53ef\u5305\u542b\u4e00\u4e2a get \u8bbf\u95ee\u5668\u3001\u4e00\u4e2a set \u8bbf\u95ee\u5668\uff0c\u6216\u8005\u540c\u65f6\u5305\u542b\u4e8c\u8005\u3002\u4f8b\u5982\uff1a<\/p>\n

\r\n\/\/ \u58f0\u660e\u7c7b\u578b\u4e3a string \u7684 Code \u5c5e\u6027\r\npublic<\/span> string<\/span> Code\r\n{\r\n   get\r\n   {\r\n      return<\/span> code;\r\n   }\r\n   set\r\n   {\r\n      code =<\/span> value<\/span>;\r\n   }\r\n}\r\n\/\/ \u58f0\u660e\u7c7b\u578b\u4e3a string \u7684 Name \u5c5e\u6027\r\npublic<\/span> string<\/span> Name\r\n{\r\n   get\r\n   {\r\n     return<\/span> name;\r\n   }\r\n   set\r\n   {\r\n     name =<\/span> value<\/span>;\r\n   }\r\n}\r\n\/\/ \u58f0\u660e\u7c7b\u578b\u4e3a int \u7684 Age \u5c5e\u6027>\r\npublic<\/span> int<\/span> Age\r\n{\r\n   get\r\n   {\r\n      return<\/span> age;\r\n   }\r\n   set\r\n   {\r\n      age =<\/span> value<\/span>;\r\n   }\r\n}<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/pre>\n
\u5b9e\u4f8b<\/strong><\/div>\n

\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u5c5e\u6027\uff08Property\uff09\u7684\u7528\u6cd5\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nusing<\/span> System<\/span>;\r\nnamespace<\/span> runoob\r\n{\r\n   class<\/span> Student\r\n   {\r\n      private<\/span> string<\/span> code =<\/span> \"N.A\"<\/span>;\r\n      private<\/span> string<\/span> name =<\/span> \"not known\"<\/span>;\r\n      private<\/span> int<\/span> age =<\/span> 0<\/span>;\r\n      \/\/ \u58f0\u660e\u7c7b\u578b\u4e3a string \u7684 Code \u5c5e\u6027\r\n      public<\/span> string<\/span> Code
\r\n      {\r\n         get\r\n         {\r\n            return<\/span> code;\r\n         }\r\n         set\r\n         {\r\n            code =<\/span> value<\/span>;\r\n         }\r\n      }\r\n   \r\n      \/\/ \u58f0\u660e\u7c7b\u578b\u4e3a string \u7684 Name \u5c5e\u6027\r\n      public<\/span> string<\/span> Name
\r\n      {\r\n         get\r\n         {\r\n            return<\/span> name;\r\n         }\r\n         set\r\n         {\r\n            name =<\/span> value<\/span>;\r\n         }\r\n      }\r\n      \/\/ \u58f0\u660e\u7c7b\u578b\u4e3a int \u7684 Age \u5c5e\u6027\r\n      public<\/span> int<\/span> Age\r\n      {\r\n         get\r\n         {\r\n            return<\/span> age;\r\n         }\r\n         set\r\n         {\r\n            age =<\/span> value<\/span>;\r\n         }\r\n      }\r\n      public<\/span> override<\/span> string<\/span> ToString(<\/span>)\r\n      {\r\n         return<\/span> \"Code = \"<\/span> +<\/span> Code +<\/span>\", Name = \"<\/span> +<\/span> Name +<\/span> \", Age = \"<\/span> +<\/span> Age;\r\n      }\r\n    }\r\n    class<\/span> ExampleDemo\r\n    {\r\n      public<\/span> static<\/span> void<\/span> Main(<\/span>)\r\n      {\r\n         \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Student \u5bf9\u8c61\r\n         Student s =<\/span> new<\/span> Student(<\/span>)<\/span>;\r\n            \r\n         \/\/ \u8bbe\u7f6e student \u7684 code\u3001name \u548c age\r\n         s.<\/span>Code<\/span> =<\/span> \"001\"<\/span>;\r\n         s.<\/span>Name<\/span> =<\/span> \"Zara\"<\/span>;\r\n         s.<\/span>Age<\/span> =<\/span> 9<\/span>;\r\n         Console.<\/span>WriteLine<\/span>(<\/span>\"Student Info: {0}\"<\/span>, s)<\/span>;\r\n         \/\/ \u589e\u52a0\u5e74\u9f84\r\n         s.<\/span>Age<\/span> +=<\/span> 1<\/span>;\r\n         Console.<\/span>WriteLine<\/span>(<\/span>\"Student Info: {0}\"<\/span>, s)<\/span>;\r\n         Console.<\/span>ReadKey<\/span>(<\/span>)<\/span>;\r\n       }\r\n   }\r\n}<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/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

Student Info: Code = 001, Name = Zara, Age = 9\r\nStudent Info: Code = 001, Name = Zara, Age = 10<\/pre>\n
\u62bd\u8c61\u5c5e\u6027\uff08Abstract Properties\uff09<\/strong><\/div>\n

\u62bd\u8c61\u7c7b\u53ef\u62e5\u6709\u62bd\u8c61\u5c5e\u6027\uff0c\u8fd9\u4e9b\u5c5e\u6027\u5e94\u5728\u6d3e\u751f\u7c7b\u4e2d\u88ab\u5b9e\u73b0\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u8bf4\u660e\u4e86\u8fd9\u70b9\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nnamespace runoob\r\n{\r\n   public abstract class Person\r\n   {\r\n      public abstract string Name\r\n      {\r\n         get;\r\n         set;\r\n      }\r\n      public abstract int Age\r\n      {\r\n         get;\r\n         set;\r\n      }\r\n   }\r\n   class Student : Person\r\n   {\r\n\r\n      private string code = \"N.A\";\r\n      private string name = \"N.A\";\r\n      private int age = 0;\r\n\r\n      \/\/ \u58f0\u660e\u7c7b\u578b\u4e3a string \u7684 Code \u5c5e\u6027\r\n      public string Code\r\n      {\r\n         get\r\n         {\r\n            return code;\r\n         }\r\n         set\r\n         {\r\n            code = value;\r\n         }\r\n      }\r\n   \r\n      \/\/ \u58f0\u660e\u7c7b\u578b\u4e3a string \u7684 Name \u5c5e\u6027\r\n      public override string Name\r\n      {\r\n         get\r\n         {\r\n            return name;\r\n         }\r\n         set\r\n         {\r\n            name = value;\r\n         }\r\n      }\r\n\r\n      \/\/ \u58f0\u660e\u7c7b\u578b\u4e3a int \u7684 Age \u5c5e\u6027\r\n      public override int Age\r\n      {\r\n         get\r\n         {\r\n            return age;\r\n         }\r\n         set\r\n         {\r\n            age = value;\r\n         }\r\n      }\r\n      public override string ToString()\r\n      {\r\n         return \"Code = \" + Code +\", Name = \" + Name + \", Age = \" + Age;\r\n      }\r\n   }\r\n   class ExampleDemo\r\n   {\r\n      public static void Main()\r\n      {\r\n         \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Student \u5bf9\u8c61\r\n         Student s = new Student();\r\n           \r\n         \/\/ \u8bbe\u7f6e student \u7684 code\u3001name \u548c age\r\n         s.Code = \"001\";\r\n         s.Name = \"Zara\";\r\n         s.Age = 9;\r\n         Console.WriteLine(\"Student Info:- {0}\", s);\r\n         \/\/ \u589e\u52a0\u5e74\u9f84\r\n         s.Age += 1;\r\n         Console.WriteLine(\"Student Info:- {0}\", s);\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

Student Info: Code = 001, Name = Zara, Age = 9\r\nStudent Info: Code = 001, Name = Zara, Age = 10<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5c5e\u6027\uff08Property\uff09\u662f\u57df\uff08Field\uff09\u7684\u6269\u5c55\uff0c\u4e14\u53ef\u4f7f\u7528\u76f8\u540c\u7684\u8bed\u6cd5\u6765\u8bbf\u95ee\u3002\u5b83\u4eec\u4f7f\u7528 \u8bbf\u95ee\u5668\uff08accessor […]<\/p>\n","protected":false},"author":321,"featured_media":245897,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-245892","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\/245892","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=245892"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/245892\/revisions"}],"predecessor-version":[{"id":245898,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/245892\/revisions\/245898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/245897"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=245892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=245892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=245892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}