隨風(fēng)設(shè)計(jì)

          隨風(fēng)設(shè)計(jì)

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評(píng)論 :: 0 Trackbacks

          本教程展示如何實(shí)現(xiàn)使用索引屬性的類。索引屬性使您可以使用表示類似于數(shù)組的若干種不同事物的集合的類。學(xué)習(xí)本教程以前應(yīng)完成索引器教程

          示例文件

          請(qǐng)參見(jiàn)“索引屬性”示例以下載和生成本教程中討論的示例文件。

          教程

          假定您要編寫(xiě)一個(gè) Document 類,該類封裝非常長(zhǎng)的文本章節(jié)。為能夠方便地實(shí)現(xiàn)各種操作(如檢查拼寫(xiě)),您可能希望以單詞(以及字符)的虛擬數(shù)組形式查看文檔。

          下面的示例展示實(shí)現(xiàn)這種類的技術(shù)。對(duì)于每個(gè)“索引屬性”,您定義一個(gè)嵌套類,該類包含對(duì)主類實(shí)例的反向引用。主類上的 readonly 字段提供對(duì)嵌套類(定義每個(gè)虛擬數(shù)組)的實(shí)例的訪問(wèn)。每個(gè)嵌套類定義一個(gè)索引器以及其他類似集合的方法(例如 Count 屬性)。下面的示例針對(duì)“Words”和“Characters”展示這一點(diǎn)。

          注意???請(qǐng)慎重使用該技術(shù)!僅在使用數(shù)組索引操作提供的抽象化能明確闡明使用您的類的代碼,并且索引器同時(shí)具有“獲取”(Get) 和“設(shè)置”(Set) 訪問(wèn)器時(shí),才使用該模式。

          示例

          本示例中定義了 Document 類。使用 WordsCharacters 這兩個(gè)索引屬性在 Document 對(duì)象上執(zhí)行某些文本操作。

          // indexedproperty.cs
          using System;
          
          public class Document
          {
              // Type allowing the document to be viewed like an array of words:
              public class WordCollection
              {
                  readonly Document document;  // The containing document
          
                  internal WordCollection(Document d)
                  {
                     document = d;
                  }
          
                  // Helper function -- search character array "text", starting at
                  // character "begin", for word number "wordCount." Returns false
                  // if there are less than wordCount words. Sets "start" and
                  // length" to the position and length of the word within text:
                  private bool GetWord(char[] text, int begin, int wordCount, 
                                                 out int start, out int length) 
                  { 
                      int end = text.Length;
                      int count = 0;
                      int inWord = -1;
                      start = length = 0; 
          
                      for (int i = begin; i <= end; ++i) 
                      {
                          bool isLetter = i < end && Char.IsLetterOrDigit(text[i]);
          
                          if (inWord >= 0) 
                          {
                              if (!isLetter) 
                              {
                                  if (count++ == wordCount) 
                                  {
                                      start = inWord;
                                      length = i - inWord;
                                      return true;
                                  }
                                  inWord = -1;
                              }
                          }
                          else 
                          {
                              if (isLetter)
                                  inWord = i;
                          }
                      }
                      return false;
                  }
          
                  // Indexer to get and set words of the containing document:
                  public string this[int index] 
                  {
                      get 
                      { 
                          int start, length;
                          if (GetWord(document.TextArray, 0, index, out start, 
                                                                    out length))
                              return new string(document.TextArray, start, length);
                          else
                              throw new IndexOutOfRangeException();
                      }
                      set 
                      {
                          int start, length;
                          if (GetWord(document.TextArray, 0, index, out start, 
                                                                   out length)) 
                          {
                              // Replace the word at start/length with the 
                              // string "value":
                              if (length == value.Length) 
                              {
                                  Array.Copy(value.ToCharArray(), 0, 
                                           document.TextArray, start, length);
                              }
                              else 
                              {
                                  char[] newText = 
                                      new char[document.TextArray.Length + 
                                                     value.Length - length];
                                  Array.Copy(document.TextArray, 0, newText, 
                                                                  0, start);
                                  Array.Copy(value.ToCharArray(), 0, newText, 
                                                       start, value.Length);
                                  Array.Copy(document.TextArray, start + length,
                                             newText, start + value.Length,
                                            document.TextArray.Length - start
                                                                      - length);
                                  document.TextArray = newText;
                              }
                          }                    
                          else
                              throw new IndexOutOfRangeException();
                      }
                  }
          
                  // Get the count of words in the containing document:
                  public int Count 
                  {
                      get 
                      { 
                          int count = 0, start = 0, length = 0;
                          while (GetWord(document.TextArray, start + length, 0, 
                                                        out start, out length))
                              ++count;
                          return count; 
                      }
                  }
              }
          
              // Type allowing the document to be viewed like an "array" 
              // of characters:
              public class CharacterCollection
              {
                  readonly Document document;  // The containing document
          
                  internal CharacterCollection(Document d)
                  {
                    document = d; 
                  }
          
                  // Indexer to get and set characters in the containing document:
                  public char this[int index] 
                  {
                      get 
                      { 
                          return document.TextArray[index]; 
                      }
                      set 
                      { 
                          document.TextArray[index] = value; 
                      }
                  }
          
                  // Get the count of characters in the containing document:
                  public int Count 
                  {
                      get 
                      { 
                          return document.TextArray.Length; 
                      }
                  }
              }
          
              // Because the types of the fields have indexers, 
              // these fields appear as "indexed properties":
              public readonly WordCollection Words;
              public readonly CharacterCollection Characters;
          
              private char[] TextArray;  // The text of the document. 
          
              public Document(string initialText)
              {
                  TextArray = initialText.ToCharArray();
                  Words = new WordCollection(this);
                  Characters = new CharacterCollection(this);
              }
          
              public string Text 
              {
                  get 
                  { 
                     return new string(TextArray); 
                  }
              }
          }
          
          class Test
          {
              static void Main()
              {
                  Document d = new Document(
                     "peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"
                  );
          
                  // Change word "peter" to "penelope":
                  for (int i = 0; i < d.Words.Count; ++i) 
                  {
                      if (d.Words[i] == "peter") 
                          d.Words[i] = "penelope";
                  }
          
                  // Change character "p" to "P"
                  for (int i = 0; i < d.Characters.Count; ++i) 
                  {
                      if (d.Characters[i] == 'p')
                          d.Characters[i] = 'P';
                  }
                  
                  Console.WriteLine(d.Text);
              }
          }

          輸出

          PeneloPe PiPer Picked a Peck of Pickled PePPers. How many Pickled PePPers did PeneloPe PiPer Pick?

          posted on 2006-08-17 11:39 曹賢 閱讀(259) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 永胜县| 安仁县| 财经| 蒙自县| 观塘区| 犍为县| 咸宁市| 礼泉县| 天祝| 曲靖市| 平陆县| 南京市| 鸡西市| 和平县| 镇巴县| 江口县| 衡阳市| 禹州市| 扬州市| 尚志市| 双鸭山市| 襄汾县| 东乡族自治县| 太仆寺旗| 台中市| 高密市| 保德县| 宝兴县| 胶南市| 定结县| 蓬莱市| 特克斯县| 巴南区| 赤城县| 信丰县| 大新县| 广安市| 伊宁县| 佛学| 全椒县| 阳东县|