{"id":3496,"date":"2025-04-13T13:15:37","date_gmt":"2025-04-13T05:15:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3496"},"modified":"2025-04-13T13:15:37","modified_gmt":"2025-04-13T05:15:37","slug":"c-%e5%b0%81%e8%a3%85%ef%bc%88encapsulation%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e5%b0%81%e8%a3%85%ef%bc%88encapsulation%ef%bc%89\/","title":{"rendered":"C# \u5c01\u88c5\uff08Encapsulation\uff09"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282\u4f1a\u5168\u9762\u8be6\u7ec6\u7684\u8bb2\u89e3 <strong>C# \u5c01\u88c5\uff08Encapsulation\uff09<\/strong> \uff0c\u5305\u542b\u5b9a\u4e49\u3001\u539f\u7406\u3001\u8bed\u6cd5\u3001\u793a\u4f8b\u3001\u6ce8\u610f\u4e8b\u9879\u53ca\u62d3\u5c55\u7406\u89e3\uff0c\u5e76\u63d0\u4f9b\u591a\u9014\u5f84\u5b9e\u73b0\u4e0e\u53c2\u8003\u94fe\u63a5\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\u4ec0\u4e48\u662f\u5c01\u88c5\uff08Encapsulation\uff09<\/h2>\n\n\n\n<p><strong>\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u56db\u5927\u57fa\u672c\u7279\u6027\u4e4b\u4e00<\/strong>\uff0c\u5176\u6838\u5fc3\u76ee\u7684\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u201c<strong>\u5c06\u6570\u636e\uff08\u5b57\u6bb5\uff09\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\u5e76\u63a7\u5236\u5916\u90e8\u8bbf\u95ee\u6743\u9650<\/strong>\u3002\u201d<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5c01\u88c5\u7684\u6838\u5fc3\u8981\u7d20<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8981\u7d20<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><strong>\u8bbf\u95ee\u4fee\u9970\u7b26<\/strong><\/td><td>\u5982 <code>private<\/code>, <code>public<\/code>, <code>protected<\/code>, <code>internal<\/code>\uff0c\u7528\u4e8e\u9650\u5236\u6210\u5458\u7684\u53ef\u89c1\u6027\u3002<\/td><\/tr><tr><td><strong>\u5b57\u6bb5\uff08Field\uff09<\/strong><\/td><td>\u901a\u5e38\u8bbe\u7f6e\u4e3a <code>private<\/code>\uff0c\u9632\u6b62\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\u3002<\/td><\/tr><tr><td><strong>\u5c5e\u6027\uff08Property\uff09<\/strong><\/td><td>\u63d0\u4f9b <code>get<\/code> \u548c <code>set<\/code> \u65b9\u6cd5\u4f5c\u4e3a\u5b57\u6bb5\u7684\u8bbf\u95ee\u63a5\u53e3\uff0c\u63a7\u5236\u8bfb\u5199\u884c\u4e3a\u3002<\/td><\/tr><tr><td><strong>\u65b9\u6cd5\uff08Method\uff09<\/strong><\/td><td>\u64cd\u4f5c\u5c01\u88c5\u5bf9\u8c61\u6570\u636e\u7684\u884c\u4e3a\uff0c\u901a\u5e38\u4e5f\u8bbe\u7f6e\u8bbf\u95ee\u6743\u9650\u3002<\/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\">\u4e09\u3001\u5c01\u88c5\u8bed\u6cd5\u5b9e\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Person\n{\n    \/\/ \u79c1\u6709\u5b57\u6bb5 - \u5c01\u88c5\u7684\u6570\u636e\n    private string name;\n    private int age;\n\n    \/\/ \u516c\u5171\u5c5e\u6027 - \u5bf9\u5916\u66b4\u9732\u63a7\u5236\u63a5\u53e3\n    public string Name\n    {\n        get { return name; }\n        set { name = value; }\n    }\n\n    public int Age\n    {\n        get \n        {\n            return age; \n        }\n        set \n        {\n            if (value &gt;= 0) \n                age = value;\n        }\n    }\n\n    \/\/ \u5c01\u88c5\u65b9\u6cd5\n    public void Introduce()\n    {\n        Console.WriteLine($\"Hi, I'm {name}, {age} years old.\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u5916\u90e8\u53ea\u80fd\u901a\u8fc7 <code>Name<\/code> \u548c <code>Age<\/code> \u5c5e\u6027\u8bbf\u95ee\u6570\u636e\uff0c\u76f4\u63a5\u8bbf\u95ee <code>name<\/code>\/<code>age<\/code> \u5b57\u6bb5\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u8bbf\u95ee\u4fee\u9970\u7b26\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4fee\u9970\u7b26<\/th><th>\u5f53\u524d\u7c7b<\/th><th>\u6d3e\u751f\u7c7b<\/th><th>\u540c\u4e00\u7a0b\u5e8f\u96c6<\/th><th>\u5916\u90e8\u7a0b\u5e8f\u96c6<\/th><\/tr><\/thead><tbody><tr><td><code>private<\/code><\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><tr><td><code>protected<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><tr><td><code>internal<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><\/tr><tr><td><code>public<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><tr><td><code>protected internal<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705 (\u5728\u6d3e\u751f\u7c7b\u4e2d)<\/td><\/tr><tr><td><code>private protected<\/code><\/td><td>\u2705<\/td><td>\u2705 (\u672c\u7a0b\u5e8f\u96c6)<\/td><td>\u2705<\/td><td>\u274c<\/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\">\u4e94\u3001\u81ea\u52a8\u5b9e\u73b0\u7684\u5c5e\u6027\uff08\u7b80\u5199\u5c01\u88c5\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Student\n{\n    public string Name { get; set; } \/\/ \u81ea\u52a8\u5c01\u88c5\uff0c\u65e0\u9700\u624b\u52a8\u5b9a\u4e49\u5b57\u6bb5\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u63a8\u8350\u7528\u4e8e\u65e0\u903b\u8f91\u5904\u7406\u7684\u7b80\u5355\u6570\u636e\u5c01\u88c5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5c01\u88c5\u4e2d\u7684\u5178\u578b\u9519\u8bef\u4e0e\u8bef\u533a<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9519\u8bef\u7c7b\u578b<\/th><th>\u539f\u56e0\u4e0e\u5efa\u8bae<\/th><\/tr><\/thead><tbody><tr><td>\u5b57\u6bb5\u672a\u79c1\u6709\u5316<\/td><td>\u8fdd\u53cd\u5c01\u88c5\u539f\u5219\uff0c\u5e94\u4f7f\u7528 <code>private<\/code> \u9650\u5236\u76f4\u63a5\u8bbf\u95ee<\/td><\/tr><tr><td>\u4e0d\u52a0\u63a7\u5236\u76f4\u63a5 <code>set<\/code> \u503c<\/td><td>\u5e94\u5728 <code>set<\/code> \u4e2d\u6dfb\u52a0\u903b\u8f91\u63a7\u5236\uff08\u5982\u5408\u6cd5\u6027\u6821\u9a8c\uff09<\/td><\/tr><tr><td>\u5c06\u903b\u8f91\u76f4\u63a5\u66b4\u9732\u7ed9\u5916\u90e8<\/td><td>\u6240\u6709\u64cd\u4f5c\u5e94\u5c01\u88c5\u5728\u65b9\u6cd5\u5185\u90e8\u8fdb\u884c<\/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\">\u4e03\u3001\u8fdb\u9636\u5c01\u88c5\u6280\u5de7<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 \u53ea\u8bfb\u5c5e\u6027\uff08\u5c01\u88c5\u53ea\u8bfb\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public string Name { get; private set; }<\/code><\/pre>\n\n\n\n<p>\u5916\u90e8\u53ea\u80fd <code>get<\/code>\uff0c\u53ea\u80fd\u5728\u6784\u9020\u51fd\u6570\u6216\u7c7b\u5185\u90e8 <code>set<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 \u521d\u59cb\u5316\u5668 + \u53ea\u8bfb\u5b57\u6bb5\u5c01\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Config\n{\n    public string Version { get; } = \"1.0\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 \u901a\u8fc7\u65b9\u6cd5\u8bbf\u95ee\u79c1\u6709\u5b57\u6bb5\uff08\u884c\u4e3a\u5c01\u88c5\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>private int count;\n\npublic void Increment()\n{\n    count++;\n}\n\npublic int GetCount()\n{\n    return count;\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\">\u516b\u3001\u6743\u5a01\u8d44\u6599\u53c2\u8003<\/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\/access-modifiers\">C# \u5c01\u88c5 &#8211; Microsoft Learn<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/using-properties\">C# \u5c5e\u6027 &#8211; Microsoft Learn<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/oo-design\/\">\u9762\u5411\u5bf9\u8c61\u57fa\u7840\u6559\u7a0b &#8211; Encapsulation<\/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\">\u4e5d\u3001\u5c01\u88c5 VS \u5176\u4ed6 OOP \u7279\u6027\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>\u5c01\u88c5<\/td><td>\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282\uff0c\u66b4\u9732\u63a5\u53e3<\/td><\/tr><tr><td>\u7ee7\u627f<\/td><td>\u5b50\u7c7b\u91cd\u7528\u7236\u7c7b\u903b\u8f91<\/td><\/tr><tr><td>\u591a\u6001<\/td><td>\u4e0d\u540c\u5bf9\u8c61\u5bf9\u540c\u4e00\u63a5\u53e3\u4ea7\u751f\u4e0d\u540c\u54cd\u5e94<\/td><\/tr><tr><td>\u62bd\u8c61<\/td><td>\u5b9a\u4e49\u7edf\u4e00\u63a5\u53e3\uff0c\u5f3a\u5236\u5b50\u7c7b\u5b9e\u73b0\uff08\u5982\u63a5\u53e3\/\u62bd\u8c61\u7c7b\uff09<\/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\">\u5341\u3001\u5b9e\u7528\u573a\u666f\u4e3e\u4f8b<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 <strong>\u4e1a\u52a1\u903b\u8f91\u9a8c\u8bc1<\/strong>\uff1a<code>set<\/code> \u65f6\u63a7\u5236\u5e74\u9f84\u4e0d\u4e3a\u8d1f<\/li>\n\n\n\n<li>\u2705 <strong>\u53ea\u8bfb ID \u81ea\u52a8\u751f\u6210<\/strong><\/li>\n\n\n\n<li>\u2705 <strong>\u65e5\u5fd7\u5c01\u88c5\u7c7b<\/strong>\u4e2d\u4ec5\u5141\u8bb8\u8c03\u7528\u65b9\u6cd5\u8bb0\u5f55\u65e5\u5fd7\uff0c\u4e0d\u5141\u8bb8\u76f4\u63a5\u64cd\u4f5c\u6587\u4ef6\u53e5\u67c4<\/li>\n<\/ul>\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\u6e05\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u8282\u4f1a\u5168\u9762\u8be6\u7ec6\u7684\u8bb2\u89e3 C# \u5c01\u88c5\uff08Encapsulation\uff09 \uff0c\u5305\u542b\u5b9a\u4e49\u3001\u539f\u7406\u3001\u8bed\u6cd5\u3001\u793a\u4f8b\u3001\u6ce8\u610f\u4e8b\u9879\u53ca\u62d3\u5c55 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3497,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3496","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\/3496","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=3496"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3496\/revisions"}],"predecessor-version":[{"id":3498,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3496\/revisions\/3498"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3497"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}