ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#中的访问修饰符 简单工厂设计模式

2022-01-17 16:34:57  阅读:155  来源: 互联网

标签:C# nb 修饰符 System class NoteBook using 设计模式 public


public:公开的,共有的

private:私有的,只能在当前类的内部使用

Protected:受保护的,只能在当前类的内部以及该类的子类中使用

Internal:只能在当前程序集中访问

Protected internal:

 

1) 能够修饰类的访问修饰符只有两个:public和internal

2) 可访问性不一致

子类的访问权限不能高于父类,会暴露父类的成员

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _02访问修饰符
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Person p = new Person();
14         }
15     }
16 
17     public class Person
18     {
19         //protected string _name;
20         //public int _age;
21         //private char _gender;
22         //internal int _chinese;
23         //protected internal int _math;
24     }
25 
26     public class Student : Person
27     {
28 
29     }
30 
31     //public class Student:Person
32     //{
33 
34     //}
35 }

 

设计模式 设计这个项目的一种方式。

简单工厂设计模式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _03简单工厂设计模式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入您想要的笔记本品牌");
14             string brand = Console.ReadLine();
15             NoteBook nb = GetNoteBook(brand);
16             nb.SayHello();
17             Console.ReadKey();
18         }
19 
20 
21         /// <summary>
22         /// 简单工厂的核心 根据用户的输入创建对象赋值给父类
23         /// </summary>
24         /// <param name="brand"></param>
25         /// <returns></returns>
26         public static NoteBook GetNoteBook(string brand)
27         {
28             NoteBook nb = null;
29             switch (brand)
30             {
31                 case "Lenovo": nb = new Lenovo();
32                     break;
33                 case "IBM": nb = new IBM();
34                     break;
35                 case "Acer": nb = new Acer();
36                     break;
37                 case "Dell": nb = new Dell();
38                     break;
39             }
40             return nb;
41         }
42     }
43 
44     public abstract class NoteBook
45     {
46         public abstract void SayHello();
47     }
48 
49     public class Lenovo : NoteBook
50     {
51         public override void SayHello()
52         {
53             Console.WriteLine("我是联想笔记本,你联想也别想");
54         }
55     }
56 
57 
58     public class Acer : NoteBook
59     {
60         public override void SayHello()
61         {
62             Console.WriteLine("我是鸿基笔记本");
63         }
64     }
65 
66     public class Dell : NoteBook
67     {
68         public override void SayHello()
69         {
70             Console.WriteLine("我是戴尔笔记本");
71         }
72     }
73 
74     public class IBM : NoteBook
75     {
76         public override void SayHello()
77         {
78             Console.WriteLine("我是IBM笔记本");
79         }
80     }
81 }

 

标签:C#,nb,修饰符,System,class,NoteBook,using,设计模式,public
来源: https://www.cnblogs.com/xhj8/p/15813998.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有