{"id":1462,"date":"2025-03-08T22:15:27","date_gmt":"2025-03-08T14:15:27","guid":{"rendered":"http:\/\/codermr.com\/?p=1462"},"modified":"2025-03-09T18:15:12","modified_gmt":"2025-03-09T10:15:12","slug":"c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/","title":{"rendered":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Class Members<\/h1>\n\n\n\n<p>Fields and methods inside classes are often referred to as &#8220;Class Members&#8221;:<\/p>\n\n\n\n<p>Create a&nbsp;<code>Car<\/code>&nbsp;class with three class members:&nbsp;<strong>two fields<\/strong>&nbsp;and&nbsp;<strong>one method<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ The class\nclass MyClass\n{\n  \/\/ Class members\n  string color = \"red\";        \/\/ field <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u2588\u2588\u8fd9\u91cc\u5176\u5b9e\u5f88\u5173\u952e,\u8fd9\u91cc\u7684 color \u53eb Filed<\/mark><\/strong>\n  int maxSpeed = 200;          \/\/ field <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u2588\u2588\u9ed8\u8ba4\u8bbf\u95ee\u6743\u9650\u662f private<\/mark><\/strong>\n  public void fullThrottle()   \/\/ method\n  {\n    Console.WriteLine(\"The car is going as fast as it can!\");\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Fields<\/h2>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u2588\u2588 \u8fd9\u91cc\u5176\u5b9e\u5f88\u5173\u952e,\u6ce8\u610f\u53eb Filed \uff0c\u4f1a\u8ddf\u4ee5\u540e\u7684 Properties \u533a\u5206\uff01\uff01\uff01<\/mark><\/strong><\/p>\n\n\n\n<p>In the previous chapter, you learned that variables inside a class are called fields, and that you can access them by creating an object of the class, and by using the dot syntax (<code>.<\/code>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car \n{\n  string color = \"red\";\/\/ <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u2588\u2588\u9ed8\u8ba4\u662f private \u7684\uff0c\u53ea\u80fd\u5728\u7c7b\u91cc\u9762\u53ef\u89c1\uff01\uff01\uff01<\/mark><\/strong>\n  int maxSpeed = 200;\n\n  static void Main(string&#91;] args)\n  {\n    Car myObj = new Car();\n    Console.WriteLine(myObj.color);\u3001\u3001\n    Console.WriteLine(myObj.maxSpeed);\n  }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"186\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-11.png\" alt=\"\" class=\"wp-image-1466\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-11.png 538w, http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-11-300x104.png 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-12.png\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"389\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-12.png\" alt=\"\" class=\"wp-image-1467\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-12.png 620w, http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-12-300x188.png 300w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/a><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Constructors<\/h1>\n\n\n\n<p>A constructor is a&nbsp;<strong>special method<\/strong>&nbsp;that is used to initialize objects. The advantage of a constructor, is that <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">it is called when an object of a class is created<\/mark><\/strong>. It can be used to set initial values for fields.<\/p>\n\n\n\n<p>\u7c7b\u7684&nbsp;<strong>\u6784\u9020\u51fd\u6570<\/strong>&nbsp;\u662f\u7c7b\u7684\u4e00\u4e2a\u7279\u6b8a\u7684\u6210\u5458\u51fd\u6570\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5f53\u521b\u5efa\u7c7b\u7684\u65b0\u5bf9\u8c61\u65f6\u6267\u884c<\/mark><\/strong>\u3002<br>\u6784\u9020\u51fd\u6570\u7684\u540d\u79f0\u4e0e\u7c7b\u7684\u540d\u79f0\u5b8c\u5168\u76f8\u540c\uff0c\u5b83\u6ca1\u6709\u4efb\u4f55\u8fd4\u56de\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>Note that the constructor name must&nbsp;<strong>match the class name<\/strong>, and it cannot have a&nbsp;<strong>return type<\/strong>&nbsp;(like&nbsp;<code>void<\/code>&nbsp;or&nbsp;<code>int<\/code>).<\/p>\n\n\n\n<p>Also note that the constructor is called when the object is created.<\/p>\n\n\n\n<p>All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you. However, then you are not able to set initial values for fields.<\/p>\n\n\n\n<p><strong>Constructors save time!<\/strong>&nbsp;Take a look at the last example on this page to really understand why.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong>\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570<\/strong>\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\u3002<\/mark><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Access Modifiers \u8bbf\u95ee\u4fee\u9970\u7b26<\/h1>\n\n\n\n<p>By now, you are quite familiar with the&nbsp;<code>public<\/code>&nbsp;keyword that appears in many of our examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public string color;<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>public<\/code>&nbsp;keyword is an&nbsp;<strong>access modifier<\/strong>, which is used to <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">set the access level\/visibility for classes, fields, methods and properties<\/mark><\/strong>.<\/p>\n\n\n\n<p>C# has the following access modifiers:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Modifier<\/th><th>Description<\/th><\/tr><tr><td><code>public<\/code><\/td><td>The code is accessible for all classes<\/td><\/tr><tr><td><code>private<\/code><\/td><td>The code is only accessible within the same class<\/td><\/tr><tr><td><code>protected<\/code><\/td><td>The code is accessible within the same class, or in a class that is inherited from that class. You will learn more about&nbsp;<a href=\"https:\/\/www.w3schools.com\/cs\/cs_inheritance.php\">inheritance<\/a>&nbsp;in a later chapter<\/td><\/tr><tr><td><code>internal<\/code><\/td><td>The code is only accessible within its own assembly, but not from another assembly. You will learn more about this in a later chapter<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">C# \u4e2d\u8bbf\u95ee\u4fee\u9970\u7b26<\/figcaption><\/figure>\n\n\n\n<p>There&#8217;s also two combinations:&nbsp;<code>protected internal<\/code>&nbsp;and&nbsp;<code>private protected<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">C#&nbsp;Properties (Get and Set)  \u2588\u2588 \u91cd\u70b9\uff01\uff01\uff01<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">Properties and Encapsulation<\/h2>\n\n\n\n<p>Before we start to explain properties, you should have a basic understanding of &#8220;<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Encapsulation<\/mark><\/strong>&#8220;. <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\uff08\u5c01\u88c5\uff09<\/mark><\/strong><\/p>\n\n\n\n<p>The meaning of&nbsp;<strong>Encapsulation<\/strong>, is to make sure that &#8220;sensitive&#8221; data is hidden from users. To achieve this, you must:<\/p>\n\n\n\n<ul>\n<li>declare fields\/variables as&nbsp;<code>private<\/code><\/li>\n\n\n\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">provide&nbsp;<code>public<\/code>&nbsp;<code>get<\/code>&nbsp;and&nbsp;<code>set<\/code>&nbsp;methods, through&nbsp;properties, to access and update the value of a&nbsp;<code>private<\/code>&nbsp;field<\/mark><\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Properties<\/h2>\n\n\n\n<p>You learned from the previous chapter that&nbsp;<code>private<\/code>&nbsp;variables can only be accessed within the same class (an outside class has no access to it). However, sometimes we need to access them &#8211; and it can be done with properties.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">A property is like a combination of a variable and a method<\/mark><\/strong>\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4e00\u4e2a\u5c5e\u6027\uff0c\u5c31\u50cf\u662f\u4e00\u4e2a\u201c\u53d8\u91cf\u201d\u548c\u201c\u65b9\u6cd5\u201d\u7684\u7ec4\u5408<\/mark><\/strong> <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">===\u300b\u8fd9\u4e2a\u89e3\u91ca\u5f88\u91cd\u8981\uff01\uff01\uff01<\/mark><\/strong>\uff09, and it has two methods: a&nbsp;<code>get<\/code>&nbsp;and a&nbsp;<code>set<\/code>&nbsp;method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n  private string name; \/\/ field\n\n  public string Name   \/\/ property\n  {\n    get { return name; }   \/\/ get method\n    set { name = value; }  \/\/ set method\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u5173\u4e8e\u201c\u5c5e\u6027 properties\u201d\u7684\u7406\u89e3\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-13.png\"><img loading=\"lazy\" decoding=\"async\" width=\"851\" height=\"271\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-13.png\" alt=\"\" class=\"wp-image-1472\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-13.png 851w, http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-13-300x96.png 300w, http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-13-768x245.png 768w\" sizes=\"(max-width: 851px) 100vw, 851px\" \/><\/a><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5982\u4e0a\u56fe\u6240\u793a\uff0cName \u8fd9\u4e2a&#8221;\u5c5e\u6027 property&#8221;\u5173\u8054\u5230\u4e86 name \u8fd9\u4e2a&#8221;\u5b57\u6bb5 filed&#8221;\u4e0a\u9762\u3002<\/mark><\/strong><br><strong>\u53ef\u4ee5\u770b\u5230\uff0c\u539f\u5148\u5b9a\u4e49\u7684\u4e13\u95e8\u7528\u6765 get \u548c set <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">&#8220;\u5b57\u6bb5 filed&#8221;<\/mark><\/strong><\/strong> <strong>\u7684 public \u65b9\u6cd5\u6ca1\u6709\u4e86\uff01\uff01\u800c\u662f\u7528 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">&#8220;\u5c5e\u6027 property&#8221;<\/mark> \u6765 get \u548c set <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">&#8220;\u5b57\u6bb5 filed&#8221;<\/mark>\u3002<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>Name<\/code>&nbsp;property is associated with the&nbsp;<code>name<\/code>&nbsp;field. <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter<\/mark>\u3010\u7528\u76f8\u540c\u7684\u540d\u5b57\u3001\u4f46\u9996\u5b57\u6bcd\u5927\u5c0f\u5199\u6765\u533a\u5206 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">&#8220;\u5c5e\u6027 property&#8221;<\/mark> \u548c <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">&#8220;\u5b57\u6bb5 filed&#8221;<\/mark> \u662f\u4e00\u4e2a\u597d\u7684\u65b9\u5f0f\uff01\u3011.<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>get<\/code>&nbsp;method returns the value of the variable&nbsp;<code>name<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>set<\/code>&nbsp;method assigns a&nbsp;<code>value<\/code>&nbsp;to the&nbsp;<code>name<\/code>&nbsp;variable. The&nbsp;<code>value<\/code>&nbsp;keyword represents the value we assign to the property.<\/p>\n\n\n\n<p><span style=\"color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\">Now we can use the<span>&nbsp;<\/span><\/span><code class=\"w3-codespan\" style=\"box-sizing: inherit; font-family: Consolas, Menlo, &quot;courier new&quot;, monospace; font-size: 15.75px; color: crimson; background-color: rgba(222, 222, 222, 0.3); padding-left: 4px; padding-right: 4px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;\">Name<\/code><span style=\"color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\"><span>&nbsp;<\/span>property to access and update the<span>&nbsp;<\/span><\/span><code class=\"w3-codespan\" style=\"box-sizing: inherit; font-family: Consolas, Menlo, &quot;courier new&quot;, monospace; font-size: 15.75px; color: crimson; background-color: rgba(222, 222, 222, 0.3); padding-left: 4px; padding-right: 4px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;\">private<\/code><span style=\"color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\"><span>&nbsp;<\/span>field of the<span>&nbsp;<\/span><\/span><code class=\"w3-codespan\" style=\"box-sizing: inherit; font-family: Consolas, Menlo, &quot;courier new&quot;, monospace; font-size: 15.75px; color: crimson; background-color: rgba(222, 222, 222, 0.3); padding-left: 4px; padding-right: 4px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;\">Person<\/code><span style=\"color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\"><span>&nbsp;<\/span>class:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n  private string name; \/\/ field\n  public string Name   \/\/ property\n  {\n    get { return name; }\n    set { name = value; }\n  }\n}\n\nclass Program\n{\n  static void Main(string&#91;] args)\n  {\n    Person myObj = new Person();\n    myObj.Name = \"Liam\"; <strong>\/\/\u2588\u2588\u2588\u64cd\u4f5c <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\"\u5c5e\u6027 property\"<\/mark> \u7528\u70b9 . \u7b26\u53f7\uff01\uff01\uff01<\/strong>\n    Console.WriteLine(myObj.Name);\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Automatic Properties (Short Hand)<\/h2>\n\n\n\n<p>C# also provides a way to use <strong>short-hand \/ automatic properties<\/strong>, <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">where you do not have to define the field for the property<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">\u3010\u4f60\u4e0d\u7528\u5b9a\u4e49&#8221;\u5c5e\u6027field&#8221;\u4e86\u3011<\/mark><\/strong>, and you only have to write&nbsp;<code>get;<\/code>&nbsp;and&nbsp;<code>set;<\/code>&nbsp;inside the property.<\/p>\n\n\n\n<p>The following example will produce the same result as the example above. The only difference is that there is less code:<\/p>\n\n\n\n<p>Using automatic properties:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n  public string Name  \/\/ property\n  { get; set; }\n}\n\nclass Program\n{\n  static void Main(string&#91;] args)\n  {\n    Person myObj = new Person();\n    myObj.Name = \"Liam\";\n    Console.WriteLine(myObj.Name);\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u66f4\u7b80\u5316\u7684\u5b9a\u4e49\u201c\u5c5e\u6027\u201d\u7684\u65b9\u5f0f\uff1a<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-14.png\"><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"430\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-14.png\" alt=\"\" class=\"wp-image-1476\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-14.png 656w, http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/image-14-300x197.png 300w\" sizes=\"(max-width: 656px) 100vw, 656px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Why Encapsulation?<\/h2>\n\n\n\n<ul>\n<li>Better control of class members (reduce the possibility of yourself (or others) to mess up the code)<\/li>\n\n\n\n<li>Fields can be made&nbsp;<strong>read-only<\/strong>&nbsp;(if you only use the&nbsp;<code>get<\/code>&nbsp;method), or&nbsp;<strong>write-only<\/strong>&nbsp;(if you only use the&nbsp;<code>set<\/code>&nbsp;method)<\/li>\n\n\n\n<li>Flexible: the programmer can change one part of the code without affecting other parts<\/li>\n\n\n\n<li>Increased security of data<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">\u518d\u8bf4\u5c5e\u6027<\/h1>\n\n\n\n<p>C# \u4e2d\u7684\u5c5e\u6027\uff08Property\uff09\u662f\u7c7b\u548c\u7ed3\u6784\u4f53\u4e2d\u7528\u4e8e\u5c01\u88c5\u6570\u636e\u7684\u6210\u5458\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u5f0f\u6765\u5b9a\u4e49\u7c7b\u6210\u5458\u7684\u8bbf\u95ee\u548c\u8bbe\u7f6e\u89c4\u5219\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u901a\u5e38\u7528\u4e8e\u9690\u85cf\u5b57\u6bb5\uff08Fields\uff09\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u540c\u65f6\u63d0\u4f9b\u63a7\u5236\u6570\u636e\u8bbf\u95ee\u7684\u673a\u5236<\/mark><\/strong>\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5c5e\u6027\u53ef\u4ee5\u770b\u4f5c\u662f\u5bf9&#8221;<em>\u5b57\u6bb5\u7684\u5305\u88c5\u5668&#8221;<\/em>\uff0c\u901a\u5e38\u7531 get \u548c set \u8bbf\u95ee\u5668\u7ec4\u6210\u3002<\/mark><\/strong><\/p>\n\n\n\n<p>\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&nbsp;<strong>\u8bbf\u95ee\u5668\uff08accessors\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>\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\n\n\n<pre class=\"wp-block-code\"><code>public class Person\n{\n    private string name;\n\n    public string Name\n    {\n        get { return name; }\n        set { name = value; }\n    }\n    <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\/\/\u2588\u2588\u2588\u2588\u4e0a\u9762\u7684\u201c\u5c5e\u6027\u7684\u8bbf\u95ee\u5668\u201d\u53ef\u4ee5\u7b80\u5316\u6210\u8fd9\u6837\uff1a<\/mark><\/strong>\n    public string Name { get; set; }\n}<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><code>Name<\/code>\u00a0\u5c5e\u6027\u5c01\u88c5\u4e86\u79c1\u6709\u5b57\u6bb5\u00a0<code>name<\/code><\/mark><\/strong>\u3002<code>get<\/code>\u00a0\u8bbf\u95ee\u5668\u7528\u4e8e\u83b7\u53d6\u5b57\u6bb5\u503c\uff0c\u800c\u00a0<code>set<\/code>\u00a0\u8bbf\u95ee\u5668\u7528\u4e8e\u8bbe\u7f6e\u5b57\u6bb5\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u52a8\u5b9e\u73b0\u7684\u5c5e\u6027<\/h3>\n\n\n\n<p class=\"has-medium-font-size\">\u5982\u679c\u4f60\u53ea\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u7684\u5c5e\u6027\uff0cC# \u5141\u8bb8\u4f7f\u7528<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u81ea\u52a8\u5b9e\u73b0\u7684\u5c5e\u6027<\/mark><\/strong>\uff0c\u8fd9\u6837<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u5b57\u6bb5<\/mark>\uff01\uff01\uff01 \u2588\u2588<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Person\n{\n    public string Name { get; set; }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4e3a Name \u5c5e\u6027\u751f\u6210\u4e00\u4e2a\u79c1\u6709\u7684\u533f\u540d\u5b57\u6bb5\u6765\u5b58\u50a8\u503c\uff01\uff01\uff01\uff01<\/mark><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ea\u8bfb\u5c5e\u6027<\/h3>\n\n\n\n<p>\u5982\u679c<strong>\u4f60\u53ea\u9700\u8981\u4e00\u4e2a\u53ea\u8bfb\u5c5e\u6027<\/strong>\uff0c\u53ef\u4ee5\u7701\u7565 set \u8bbf\u95ee\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Person\n{\n    public string Name { get; }<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\/\/\u2588\u2588\u53ea\u8bfb\u5c5e\u6027\uff1a\u53ea\u6709\u63d0\u4f9b get; \u8bbf\u95ee\u5668<\/mark><\/strong>\n\n    public Person(string name)\n    {\n        Name = name;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ea\u5199\u5c5e\u6027<\/h3>\n\n\n\n<p>\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u4f60\u53ea\u9700\u8981\u4e00\u4e2a\u53ea\u5199\u5c5e\u6027\uff0c\u53ef\u4ee5\u7701\u7565 get \u8bbf\u95ee\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Person<br>{<br>    private string name;<br><br>    public string Name<br>    {<br>        set { name = value; }<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\/\/\u2588\u2588\u53ea\u5199\u5c5e\u6027\uff1a\u53ea\u6709\u63d0\u4f9b set; \u8bbf\u95ee\u5668<\/mark><\/strong><br>    }<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u903b\u8f91<\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 get \u548c set \u8bbf\u95ee\u5668\u4e2d\u5305\u542b\u81ea\u5b9a\u4e49\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Person<br>{<br>    private string name;<br><br>    public string Name<br>    {<br>        get { return name; }<br>        set   <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\/\/\u2588\u2588\u5728 set; \u8bbf\u95ee\u5668\u4e2d\u81ea\u5b9a\u4e49\u903b\u8f91<\/mark><\/strong><br>        {<br>            if (string.IsNullOrWhiteSpace(value))<br>                throw new ArgumentException(\"Name cannot be empty.\");<br>            name = value;<br>        }<br>    }<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8ba1\u7b97\u5c5e\u6027<\/h3>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5c5e\u6027\u4e5f\u53ef\u4ee5\u662f\u8ba1\u7b97\u7684\uff0c\u4e0d\u4f9d\u8d56\u4e8e\u5b57\u6bb5\u3002\u2588\u2588<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Rectangle<br>{<br>    public int Width { get; set; }<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\/\/\u2588\u2588\u5c5e\u6027<\/mark><\/strong><br>    public int Height { get; set; }<br><br>    public int Area<br>    {<br>        get { return Width * Height; }<br>    }<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u62bd\u8c61\u5c5e\u6027\uff08Abstract Properties\uff09<\/h2>\n\n\n\n<p>\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\n\n\n<pre class=\"wp-block-code\"><code>using System;\nnamespace Runoob\n{\n    public abstract class Person\n    {\n        public abstract string Name { get; set; }\n        public abstract int Age { get; set; }\n    }\n\n    class Student : Person\n    {\n        \/\/ \u58f0\u660e\u81ea\u52a8\u5b9e\u73b0\u7684\u5c5e\u6027\n        public string Code { get; set; } = \"N.A\";\n        public override string Name { get; set; } = \"N.A\";\n        public override int Age { get; set; } = 0;\n\n        public override string ToString()\n        {\n            return $\"Code = {Code}, Name = {Name}, Age = {Age}\";\n        }\n    }\n\n    class ExampleDemo\n    {\n        public static void Main()\n        {\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Student \u5bf9\u8c61\n            Student s = new Student\n            {\n                Code = \"001\",\n                Name = \"Zara\",\n                Age = 9\n            };\n\n            Console.WriteLine(\"Student Info:- {0}\", s);\n            \n            \/\/ \u589e\u52a0\u5e74\u9f84\n            s.Age += 1;\n            Console.WriteLine(\"Student Info:- {0}\", s);\n\n            Console.ReadKey();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\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\n\n\n<pre class=\"wp-block-code\"><code>Student Info: Code = 001, Name = Zara, Age = 9\nStudent Info: Code = 001, Name = Zara, Age = 10\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Class Members Fields and methods inside classes are oft&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1464,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[60],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027 - \u7801\u5148\u751f\u7684\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08oop\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027 - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Class Members Fields and methods inside classes are oft...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08oop\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-08T14:15:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-09T10:15:12+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/csharp1-\u526f\u672c.png\" \/>\n\t<meta property=\"og:image:width\" content=\"489\" \/>\n\t<meta property=\"og:image:height\" content=\"468\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"\u7801\u5148\u751f\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/mrcode2021\" \/>\n<meta name=\"twitter:site\" content=\"@mrcode2021\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u7801\u5148\u751f\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027\",\"datePublished\":\"2025-03-08T14:15:27+00:00\",\"dateModified\":\"2025-03-09T10:15:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/\"},\"wordCount\":726,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"keywords\":[\"csharp\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/\",\"name\":\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027 - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2025-03-08T14:15:27+00:00\",\"dateModified\":\"2025-03-09T10:15:12+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/codermr.com\/#website\",\"url\":\"http:\/\/codermr.com\/\",\"name\":\"\u7801\u5148\u751f\u7684\u535a\u5ba2\",\"description\":\"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP\",\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/codermr.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\",\"name\":\"\u7801\u5148\u751f\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"contentUrl\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"width\":400,\"height\":400,\"caption\":\"\u7801\u5148\u751f\"},\"logo\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/codermr.com\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021\"],\"url\":\"http:\/\/codermr.com\/index.php\/author\/coderma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027 - \u7801\u5148\u751f\u7684\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08oop\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027\/","og_locale":"zh_CN","og_type":"article","og_title":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027 - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"Class Members Fields and methods inside classes are oft...","og_url":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08oop\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2025-03-08T14:15:27+00:00","article_modified_time":"2025-03-09T10:15:12+00:00","og_image":[{"width":489,"height":468,"url":"http:\/\/codermr.com\/wp-content\/uploads\/2025\/03\/csharp1-\u526f\u672c.png","type":"image\/png"}],"author":"\u7801\u5148\u751f","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/mrcode2021","twitter_site":"@mrcode2021","twitter_misc":{"\u4f5c\u8005":"\u7801\u5148\u751f","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027","datePublished":"2025-03-08T14:15:27+00:00","dateModified":"2025-03-09T10:15:12+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/"},"wordCount":726,"commentCount":0,"publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"keywords":["csharp"],"articleSection":["C#"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/","url":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/","name":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027 - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2025-03-08T14:15:27+00:00","dateModified":"2025-03-09T10:15:12+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2025\/03\/08\/c%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b1%87%e6%80%bb%e4%ba%8c%ef%bc%88oop%ef%bc%89%ef%bc%9a%e5%ad%97%e6%ae%b5%e3%80%81%e5%b1%9e%e6%80%a7\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"C#\u57fa\u7840\u77e5\u8bc6\u6c47\u603b\u4e8c\uff08OOP\uff09\uff1a\u5b57\u6bb5\u3001\u5c5e\u6027"}]},{"@type":"WebSite","@id":"http:\/\/codermr.com\/#website","url":"http:\/\/codermr.com\/","name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","description":"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP","publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/codermr.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf","name":"\u7801\u5148\u751f","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/","url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","contentUrl":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","width":400,"height":400,"caption":"\u7801\u5148\u751f"},"logo":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/codermr.com","https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021"],"url":"http:\/\/codermr.com\/index.php\/author\/coderma\/"}]}},"_links":{"self":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1462"}],"collection":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/comments?post=1462"}],"version-history":[{"count":12,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1462\/revisions"}],"predecessor-version":[{"id":1505,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1462\/revisions\/1505"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media\/1464"}],"wp:attachment":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media?parent=1462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=1462"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=1462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}