{"id":3517,"date":"2025-04-13T13:39:48","date_gmt":"2025-04-13T05:39:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3517"},"modified":"2025-04-13T13:39:48","modified_gmt":"2025-04-13T05:39:48","slug":"c-%e7%b1%bb%ef%bc%88class%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e7%b1%bb%ef%bc%88class%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"C# \u7c7b\uff08Class\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>C# \u7c7b\uff08<code>class<\/code>\uff09\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u63cf\u8ff0\u4e86\u4e00\u7ec4\u5c5e\u6027\u3001\u65b9\u6cd5\u548c\u4e8b\u4ef6\u7b49\u6210\u5458\u3002\u7c7b\u4f5c\u4e3a\u5bf9\u8c61\u7684\u84dd\u56fe\uff0c\u7528\u4e8e\u5c01\u88c5\u6570\u636e\u548c\u884c\u4e3a\u3002\u901a\u8fc7\u7c7b\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u5bf9\u8c61\u8bbf\u95ee\u548c\u64cd\u4f5c\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\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\u7c7b\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7c7b\u662f\u5f15\u7528\u7c7b\u578b<\/strong>\uff1a\u7c7b\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\u5b58\u50a8\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u4f20\u9012\u7684\u662f\u5f15\u7528\uff08\u6307\u9488\uff09\u800c\u4e0d\u662f\u526f\u672c\u3002<\/li>\n\n\n\n<li><strong>\u5c01\u88c5<\/strong>\uff1a\u7c7b\u5141\u8bb8\u5c06\u6570\u636e\u548c\u884c\u4e3a\u5c01\u88c5\u5728\u4e00\u4e2a\u5355\u5143\u4e2d\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\u63a7\u5236\uff08\u5982\u901a\u8fc7\u8bbf\u95ee\u4fee\u9970\u7b26\u6765\u63a7\u5236\u5bf9\u5b57\u6bb5\u548c\u65b9\u6cd5\u7684\u8bbf\u95ee\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6784\u9020\u51fd\u6570<\/strong>\uff1a\u7c7b\u53ef\u4ee5\u5305\u542b\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u7c7b\u7684\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u7ee7\u627f<\/strong>\uff1a\u7c7b\u53ef\u4ee5\u7ee7\u627f\u5176\u4ed6\u7c7b\uff0c\u7ee7\u627f\u4f7f\u5f97\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u83b7\u53d6\u53e6\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u3002<\/li>\n\n\n\n<li><strong>\u591a\u6001<\/strong>\uff1a\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u65b9\u6cd5\u91cd\u8f7d\u548c\u65b9\u6cd5\u91cd\u5199\uff0c\u652f\u6301\u591a\u6001\u6027\u3002<\/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\">\u4e8c\u3001\u5b9a\u4e49\u7c7b<\/h2>\n\n\n\n<p>\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u9700\u8981\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\u3002\u7c7b\u4e2d\u53ef\u4ee5\u5305\u542b\u5b57\u6bb5\u3001\u5c5e\u6027\u3001\u65b9\u6cd5\u548c\u6784\u9020\u51fd\u6570\u7b49\u6210\u5458\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person\n{\n    \/\/ \u5b57\u6bb5\n    public string Name;\n    public int Age;\n\n    \/\/ \u6784\u9020\u51fd\u6570\n    public Person(string name, int age)\n    {\n        Name = name;\n        Age = age;\n    }\n\n    \/\/ \u65b9\u6cd5\n    public void Introduce()\n    {\n        Console.WriteLine($\"Hi, my name is {Name} and I am {Age} years old.\");\n    }\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\">\u4e09\u3001\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/h2>\n\n\n\n<p>\u901a\u8fc7 <code>new<\/code> \u5173\u952e\u5b57\u6765\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5b9e\u4f8b\u8bbf\u95ee\u7c7b\u7684\u6210\u5458\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Person person1 = new Person(\"John\", 30);  \/\/ \u521b\u5efa\u5bf9\u8c61\u5e76\u521d\u59cb\u5316\nperson1.Introduce();  \/\/ \u8c03\u7528\u65b9\u6cd5\nConsole.WriteLine(person1.Name);  \/\/ \u8bbf\u95ee\u5b57\u6bb5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u7c7b\u7684\u6210\u5458<\/h2>\n\n\n\n<p>\u7c7b\u53ef\u4ee5\u5305\u542b\u591a\u79cd\u6210\u5458\uff0c\u5982\u5b57\u6bb5\u3001\u5c5e\u6027\u3001\u65b9\u6cd5\u3001\u6784\u9020\u51fd\u6570\u7b49\u3002\u4e0b\u9762\u662f\u6bcf\u4e2a\u6210\u5458\u7684\u5177\u4f53\u8bf4\u660e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b57\u6bb5\uff08Field\uff09<\/h3>\n\n\n\n<p>\u5b57\u6bb5\u662f\u7c7b\u4e2d\u7684\u53d8\u91cf\uff0c\u7528\u4e8e\u5b58\u50a8\u5bf9\u8c61\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car\n{\n    public string Model;\n    public int Year;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5c5e\u6027\uff08Property\uff09<\/h3>\n\n\n\n<p>\u5c5e\u6027\u662f\u5bf9\u5b57\u6bb5\u7684\u5c01\u88c5\uff0c\u5141\u8bb8\u901a\u8fc7 <code>get<\/code> \u548c <code>set<\/code> \u65b9\u6cd5\u8bbf\u95ee\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car\n{\n    private string model;\n\n    public string Model\n    {\n        get { return model; }\n        set { model = value; }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u65b9\u6cd5\uff08Method\uff09<\/h3>\n\n\n\n<p>\u65b9\u6cd5\u662f\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u53ef\u4ee5\u6267\u884c\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator\n{\n    public int Add(int a, int b)\n    {\n        return a + b;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6784\u9020\u51fd\u6570\uff08Constructor\uff09<\/h3>\n\n\n\n<p>\u6784\u9020\u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u7c7b\u7684\u5b9e\u4f8b\u3002\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4f1a\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\uff0c\u4f46\u4f60\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car\n{\n    public string Model;\n    public int Year;\n\n    public Car(string model, int year)\n    {\n        Model = model;\n        Year = year;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u6790\u6784\u51fd\u6570\uff08Destructor\uff09<\/h3>\n\n\n\n<p>\u6790\u6784\u51fd\u6570\u5728\u5bf9\u8c61\u88ab\u5783\u573e\u56de\u6536\u65f6\u81ea\u52a8\u8c03\u7528\uff0c\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car\n{\n    ~Car()  \/\/ \u6790\u6784\u51fd\u6570\n    {\n        Console.WriteLine(\"Car object is being destroyed.\");\n    }\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\">\u4e94\u3001\u7c7b\u7684\u8bbf\u95ee\u4fee\u9970\u7b26<\/h2>\n\n\n\n<p>C# \u63d0\u4f9b\u4e86\u591a\u79cd\u8bbf\u95ee\u4fee\u9970\u7b26\u6765\u63a7\u5236\u5bf9\u7c7b\u548c\u7c7b\u6210\u5458\u7684\u8bbf\u95ee\u6743\u9650\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>public<\/code><\/strong>\uff1a\u53ef\u4ee5\u88ab\u4efb\u4f55\u7c7b\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong><code>private<\/code><\/strong>\uff1a\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong><code>protected<\/code><\/strong>\uff1a\u53ea\u80fd\u5728\u7c7b\u53ca\u5176\u5b50\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong><code>internal<\/code><\/strong>\uff1a\u53ea\u80fd\u5728\u5f53\u524d\u7a0b\u5e8f\u96c6\u4e2d\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong><code>protected internal<\/code><\/strong>\uff1a\u53ef\u4ee5\u5728\u5f53\u524d\u7a0b\u5e8f\u96c6\u4e2d\u4ee5\u53ca\u4ece\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong><code>private protected<\/code><\/strong>\uff1a\u53ea\u80fd\u5728\u5f53\u524d\u7c7b\u53ca\u5176\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car\n{\n    private string model;  \/\/ \u53ea\u80fd\u5728Car\u7c7b\u5185\u90e8\u8bbf\u95ee\n    public int year;       \/\/ \u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u8bbf\u95ee\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\">\u516d\u3001\u7c7b\u7684\u7ee7\u627f<\/h2>\n\n\n\n<p>C# \u652f\u6301\u7c7b\u7ee7\u627f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u6210\u5458\u3002\u7ee7\u627f\u8ba9\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7c7b\uff0c\u8be5\u7c7b\u5177\u5907\u7236\u7c7b\u7684\u529f\u80fd\uff0c\u5e76\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u529f\u80fd\u6216\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    public void Eat()\n    {\n        Console.WriteLine(\"Animal is eating.\");\n    }\n}\n\nclass Dog : Animal  \/\/ Dog \u7c7b\u7ee7\u627f Animal \u7c7b\n{\n    public void Bark()\n    {\n        Console.WriteLine(\"Dog is barking.\");\n    }\n}\n\nDog dog = new Dog();\ndog.Eat();  \/\/ \u7ee7\u627f\u81eaAnimal\u7c7b\u7684\u65b9\u6cd5\ndog.Bark(); \/\/ Dog\u7c7b\u81ea\u5df1\u7684\u65b9\u6cd5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u591a\u6001\u6027<\/h2>\n\n\n\n<p>\u591a\u6001\u6027\u4f7f\u5f97\u4e00\u4e2a\u7236\u7c7b\u5f15\u7528\u53ef\u4ee5\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\uff0c\u8c03\u7528\u65b9\u6cd5\u65f6\u4f1a\u6839\u636e\u5b9e\u9645\u5bf9\u8c61\u7c7b\u578b\u6267\u884c\u76f8\u5e94\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u65b9\u6cd5\u91cd\u5199\uff08Overriding\uff09<\/h3>\n\n\n\n<p>\u901a\u8fc7\u91cd\u5199\uff08<code>override<\/code>\uff09\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5b50\u7c7b\u53ef\u4ee5\u63d0\u4f9b\u81ea\u5df1\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal\n{\n    public virtual void Speak()\n    {\n        Console.WriteLine(\"Animal speaks\");\n    }\n}\n\nclass Dog : Animal\n{\n    public override void Speak()\n    {\n        Console.WriteLine(\"Dog barks\");\n    }\n}\n\nAnimal animal = new Dog();\nanimal.Speak();  \/\/ \u8f93\u51fa Dog barks<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u65b9\u6cd5\u91cd\u8f7d\uff08Overloading\uff09<\/h3>\n\n\n\n<p>\u65b9\u6cd5\u91cd\u8f7d\u662f\u6307\u540c\u540d\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u53c2\u6570\u7c7b\u578b\u6216\u6570\u91cf\u6765\u533a\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator\n{\n    public int Add(int a, int b)\n    {\n        return a + b;\n    }\n\n    public double Add(double a, double b)\n    {\n        return a + b;\n    }\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\u7c7b\u7684\u5b9e\u4f8b\u5316\u548c\u5185\u5b58\u7ba1\u7406<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7c7b\u5b9e\u4f8b\u5316<\/h3>\n\n\n\n<p>\u901a\u8fc7 <code>new<\/code> \u5173\u952e\u5b57\u6765\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5e76\u8c03\u7528\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Person person = new Person(\"Alice\", 25);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u7c7b\u7684\u5185\u5b58\u7ba1\u7406<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5806\u5185\u5b58<\/strong>\uff1a\u7c7b\u5b9e\u4f8b\u901a\u5e38\u5206\u914d\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u901a\u8fc7\u5f15\u7528\u8fdb\u884c\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u5783\u573e\u56de\u6536\uff08GC\uff09<\/strong>\uff1a\u7c7b\u5b9e\u4f8b\u7684\u5185\u5b58\u4f1a\u5728\u4e0d\u518d\u4f7f\u7528\u65f6\u7531\u5783\u573e\u56de\u6536\u5668\u56de\u6536\u3002<\/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\u53c2\u8003\u6587\u732e\u548c\u6587\u6863<\/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\/classes\">C# \u7c7b\u6587\u6863 &#8211; Microsoft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/fundamentals\/object-oriented\/\">C# \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b &#8211; Microsoft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/tutorials\/classes\">C# \u7c7b\u548c\u5bf9\u8c61\u793a\u4f8b &#8211; Microsoft<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u5f3a\u5927\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u529f\u80fd\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u6269\u5c55\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# \u7c7b\uff08class\uff09\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u63cf\u8ff0\u4e86\u4e00\u7ec4\u5c5e\u6027\u3001\u65b9\u6cd5\u548c\u4e8b\u4ef6\u7b49\u6210\u5458\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3517","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\/3517","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=3517"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3517\/revisions"}],"predecessor-version":[{"id":3519,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3517\/revisions\/3519"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3518"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}