索引器的重载

简介:
1  Company com  =   new  Company();
2  com.Departments.Add( " HR " );
3  com.Departments.Add( " Market " );
4  com.Departments.Add( " Development " );
5
6  com.Departments[ 0 ].Staffs.Add( " Alice " );
7  com.Departments[ 0 ].Staffs.Add( " Amy " );
8  com.Departments[ 0 ].Staffs.Add( " Ellen " );
9  com.Departments[ 2 ].Staffs.Add( " Albert " );
10  com.Departments[ 2 ].Staffs.Add( " Mark " );
11  com.Departments[ 2 ].Staffs.Add( " Kevin " );
12  com.Departments[ 2 ].Staffs.Add( " Neil " );
13
14  System.Console.WriteLine(com.Departments[ 0 ].Staffs[ 1 ].Name);

在循环处理的时候,第14行自然没有什么不好的地方,但如果像14行那样,要直接访问某个部门,用int的索引似乎显得太突兀。

我们优化DepartmentCollection的this索引器,重载this
 

1   public  Department  this [ int  index]
2   {
3 set
4 {
5 this.List[index] = value;
6 }

7 get
8 {
9 return (Department)this.List[index];
10 }

11 }

12
13   public  Department  this [ string  name]
14   {
15 get
16 {
17 for (int i = 0; i <= this.List.Count - 1; i++)
18 {
19 if (((Department)this.List[i]).Name == name)
20 {
21 return this[i];
22 }

23 }

24 return null;
25 }

26
27 }


然后,我们继续优化Department
 

1 public   class  Department
2   {
3 public StaffCollection Staffs = new StaffCollection();
4
5 public Department(string name)
6 {
7 Name = name;
8 }

9 public readonly string Name;
10
11 public Staff this[int index]
12 {
13 set
14 {
15 Staffs[index] = value;
16 }

17 get
18 {
19 return Staffs[index];
20 }

21 }

22 }

加了第11行的this。

现在的调用代码是
 

1  Company com  =   new  Company();
2  com.Departments.Add( " HR " );
3  com.Departments.Add( " Market " );
4  com.Departments.Add( " Development " );
5
6  com.Departments[ 0 ].Staffs.Add( " Alice " );
7  com.Departments[ 0 ].Staffs.Add( " Amy " );
8  com.Departments[ 0 ].Staffs.Add( " Ellen " );
9  com.Departments[ 2 ].Staffs.Add( " Albert " );
10  com.Departments[ 2 ].Staffs.Add( " Mark " );
11  com.Departments[ 2 ].Staffs.Add( " Kevin " );
12  com.Departments[ 2 ].Staffs.Add( " Neil " );
13
14  System.Console.WriteLine(com.Departments[ " Development " ][ 1 ].Name);


注意第14行,是不是优雅多了啊?



本文转自shyleoking 51CTO博客,原文链接:http://blog.51cto.com/shyleoking/806275

相关文章
0 和 1 的字面量
Julia 支持整数和浮点数数据类型,以及字面量表示固定值。`zero(x)` 和 `one(x)` 函数提供类型安全的零和一字面量,减少类型转换成本。例如:`zero(Float32)` 返回 `0.0f0`,`one(Int32)` 返回 `1`。
|
应用服务中间件 Linux nginx
CentOS 9 安装 Nginx 模块 `subs_filter`
sub_filter 和 subs_filter 区别 sub_filter( 0.7.24):替换响应体(Response Body)中的文本,只能设置一组替换。 subs_filter:替换响应体(Response Body)和响应头(Response Headers)中的文本,可以设置多组替换。
578 0
|
存储 缓存 内存技术
SDL开发笔记(二):音频基础介绍、使用SDL播放音频
SDL开发笔记(二):音频基础介绍、使用SDL播放音频
SDL开发笔记(二):音频基础介绍、使用SDL播放音频
|
Apache 数据安全/隐私保护 Windows
|
3天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1089 0
|
12天前
|
人工智能 运维 安全
|
2天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
382 8
|
10天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
3天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
276 0