当前位置:首页 > 全部子站 > 备课网 > 学院

C# 程序员参考--索引属性教学文章

来源:长理培训发布时间:2017-08-20 20:20:32

 本教程展示如何实现使用索引属性的类。索引属性使您可以使用表示类似于数组的若干种不同事物的集合的类。学习本教程以前应完成索引器教程。

示例文件

请参见“索引属性”示例以下载和生成本教程中讨论的示例文件。

教程

假定您要编写一个 Document 类,该类封装非常长的文本章节。为能够方便地实现各种操作(如检查拼写),您可能希望以单词(以及字符)的虚拟数组形式查看文档。

下面的示例展示实现这种类的技术。对于每个“索引属性”,您定义一个嵌套类,该类包含对主类实例的反向引用。主类上的 readonly 字段提供对嵌套类(定义每个虚拟数组)的实例的访问。每个嵌套类定义一个索引器以及其他类似集合的方法(例如 Count 属性)。下面的示例针对“Words”和“Characters”展示这一点。

注意   请慎重使用该技术!仅在使用数组索引操作提供的抽象化能明确阐明使用您的类的代码,并且索引器同时具有“获取”(Get) 和“设置”(Set) 访问器时,才使用该模式。

示例

本示例中定义了 Document 类。使用 Words 和 Characters 这两个索引属性在 Document 对象上执行某些文本操作。

// 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) 



                    {



 

 

[1] [2] [3] 下一页  

 

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';

 

上一页  [1] [2] [3] 下一页  

 

} Console.WriteLine(d.Text); } }

输出

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

 

 

 

上一页  [1] [2] [3] 

责编:杨粟梅

发表评论(共0条评论)
请自觉遵守互联网相关政策法规,评论内容只代表网友观点,发表审核后显示!

国家电网校园招聘考试直播课程通关班

  • 讲师:刘萍萍 / 谢楠
  • 课时:160h
  • 价格 4580

特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关

配套通关班送国网在线题库一套

课程专业名称
讲师
课时
查看课程

国家电网招聘考试录播视频课程

  • 讲师:崔莹莹 / 刘萍萍
  • 课时:180h
  • 价格 3580

特色解密新课程高频考点,免费学习,助力一次通关

配套全套国网视频课程免费学习

课程专业名称
讲师
课时
查看课程
在线题库
面授课程更多>>
图书商城更多>>
在线报名
  • 报考专业:
    *(必填)
  • 姓名:
    *(必填)
  • 手机号码:
    *(必填)
返回顶部