class Program
{
static void Main(string[] args)
{
//定義一個電腦配置的結構,要求結構里必須包含的成員有:方法、字段、屬性、索引器、構造函數,
//其中有一個方法是顯示當前電腦的配置信息;
Computer cp = new Computer("神舟", "黑色", "800dao");
cp.Show();
Console.WriteLine(cp[0]);//cp[0]是一個值,必須聲明一個類型去接收它或者打印
string a = cp[1];
Console.WriteLine(a);//聲明一個類型必須看它返回值的類型 string int computer
Console.ReadKey();
}
}
struct Computer
{
private string name;
private string colour;
private string price;
public string Name
{
get { return name; }
set { name = value; }
}
public string Colour
{
get { return colour; }
set { colour = value; }
}
public string Price
{
get { return price; }
set { price = value; }
}
public string this[int index]
{
set
{
switch (index)
{
case 0:
this.name = value;
break;
case 1:
this.colour = value;
break;
case 2:
this.price = value;
break;
default:
Console.WriteLine("超出范圍");
break;
}
}
get
{
string a = "";
switch (index)
{
case 0:
a = this.name;
break;
case 1:
a = this.colour;
break;
case 2:
a = this.price;
break;
default:
a = "超出范圍";
break;
}
return a;
}
}
public Computer(string _name, string _colour, string _price)
{
this.name = _name;
this.colour = _colour;
this.price = _price;
}
public void Show()
{
Console.WriteLine("名字:{0},顏色:{1},價格:{2}", this.name, this.colour, this.price);
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。