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);
}
}
网友评论