- C#
- using System;
- using System.Collections.Generic;
- namespace MarvellousWorks.PracticalPattern.Concept.Delegating
- {
- public delegate void StringAssignmentEventHandler(); //抽象的操作方法
- public class InvokeList
- {
- private IList<StringAssignmentEventHandler> handlers;
- private string[] message = new string[3];
- public InvokeList()
- {
- // 绑定一组抽象方法
- handlers = new List<StringAssignmentEventHandler>();
- handlers.Add(AppendHello);
- handlers.Add(AppendComma);
- handlers.Add(AppendWorld);
- }
- public void Invoke()
- {
- foreach (StringAssignmentEventHandler handler in handlers)
- handler();
- }
- public string this[int index] { get { return message[index]; } }
- // 具体操作方法
- public void AppendHello() { message[0] = "hello"; }
- public void AppendComma() { message[1] = ","; }
- public void AppendWorld() { message[2] = "world"; }
- }
- }
C#索引器的使用
最新推荐文章于 2024-08-27 17:49:17 发布