class Program
{
static void Main(string[] args)
{
//定義一個(gè)電腦配置的結(jié)構(gòu)冒掌,要求結(jié)構(gòu)里必須包含的成員有:方法腮猖、字段配椭、屬性灯荧、索引器靶壮、構(gòu)造函數(shù)窝撵,
//其中有一個(gè)方法是顯示當(dāng)前電腦的配置信息俺夕;
Computer cp = new Computer("神舟", "黑色", "800dao");
cp.Show();
Console.WriteLine(cp[0]);//cp[0]是一個(gè)值,必須聲明一個(gè)類型去接收它或者打印
string a = cp[1];
Console.WriteLine(a);//聲明一個(gè)類型必須看它返回值的類型 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}缤至,價(jià)格:{2}", this.name, this.colour, this.price);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者