using System;
using System.Collections.Generic; using System.Linq; using System.Text; namespace Property
{ class Program { static void Main(string[] args) { Myclass class1 = new Myclass(); for (int i = 0; i < 10; i++) { for (int j=0;j<10;j++) { class1[i * 10, j] = i * 10 + j; Console.WriteLine("No{0}{1}:{2}", i, j, class1[i * 10, j]); } Console.WriteLine(); } for (int i = 0; i < class1.StrCount; i++) { Console.WriteLine(class1[i]);//調(diào)用第二個索引 } class1.StrCount = 5;//設(shè)置StrCount值 for (int i = 0; i < class1.StrCount; i++) { Console.WriteLine(class1[i]);//調(diào)用第二個索引 } Console.WriteLine(Myclass.ClassName);//調(diào)用靜態(tài)方法時必須用類名對象調(diào)用 Console.Write("Press any key to continue"); Console.ReadLine(); }
}
class Myclass { private const int count = 100; private static int[] intArray = new int[count]; //第一個索引函數(shù),,支持讀和寫 public int this[int index, int offset] { get { if ((index + offset) > 0 && (index + offset) < count) { return intArray[index + offset]; } else return 0; } set { if ((index + offset) > 0 && (index + offset) < count) { intArray[index + offset] = value; } } } private int count1 = 3;
private string[] strArray = {"111","222","333" };//直接賦值法 //第二個索引,,只讀 public string this[int index] { get { if ((index) > 0 && (index) < count1) { return strArray[index]; } else { return "null"; } } } //實例屬性可讀可寫 public int StrCount { get { return count1; } set { if (value > count1) { strArray=new string[value]; for (int i = 0; i < value; i++) { strArray[i] = string.Format("String NO.{0}",i); } count1 = value; } } } private static string str = "MyClass"; public static string ClassName { get { return str; } } }
} |
|
來自: 修行的嘟嘟 > 《軟件開發(fā)》