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)用第二個(gè)索引
}
class1.StrCount = 5;//設(shè)置StrCount值
for (int i = 0; i < class1.StrCount; i++)
{
Console.WriteLine(class1[i]);//調(diào)用第二個(gè)索引
}
Console.WriteLine(Myclass.ClassName);//調(diào)用靜態(tài)方法時(shí)必須用類名對(duì)象調(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];
//第一個(gè)索引函數(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" };//直接賦值法
//第二個(gè)索引,只讀
public string this[int index]
{
get
{
if ((index) > 0 && (index) < count1)
{
return strArray[index];
}
else
{
return "null";
}
}
}
//實(shí)例屬性可讀可寫
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;
}
}
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。