Code
using System;
namespace _03_21
{
class Car
{
string[] wheels = new string[4];
public Car()
{
wheels[0] = "左前轮";
wheels[1] = "右前轮";
wheels[2] = "左后轮";
wheels[3] = "右后轮";
}
public string this [int index] // Car类的索引器
{
get
{
return wheels[index];
}
set
{
wheels[index] = value;
}
}
}
class Class_03_21
{
public static void Main(String[] args)
{
Car car1 = new Car();
Console.WriteLine("car1的第一个轮子是:{0}", car1[0]);
Console.WriteLine("car1的第三个轮子是:{0}", car1[2]);
car1[1] = "新的右前轮";
car1[3] = "更新的右后轮";
Console.WriteLine("car1的第二个轮子是:{0}", car1[1]);
Console.WriteLine("car1的第四个轮子是:{0}", car1[3]);
}
}
}
using System;
namespace _03_21
{
class Car
{
string[] wheels = new string[4];
public Car()
{
wheels[0] = "左前轮";
wheels[1] = "右前轮";
wheels[2] = "左后轮";
wheels[3] = "右后轮";
}
public string this [int index] // Car类的索引器
{
get
{
return wheels[index];
}
set
{
wheels[index] = value;
}
}
}
class Class_03_21
{
public static void Main(String[] args)
{
Car car1 = new Car();
Console.WriteLine("car1的第一个轮子是:{0}", car1[0]);
Console.WriteLine("car1的第三个轮子是:{0}", car1[2]);
car1[1] = "新的右前轮";
car1[3] = "更新的右后轮";
Console.WriteLine("car1的第二个轮子是:{0}", car1[1]);
Console.WriteLine("car1的第四个轮子是:{0}", car1[3]);
}
}
}
Code
using System;
namespace _03_22
{
class Car
{
string[] wheels = new string[4];
public Car()
{
wheels[0] = "左前轮";
wheels[1] = "右前轮";
wheels[2] = "左后轮";
wheels[3] = "右后轮";
}
public string this [int index] // Car类的索引器
{
get
{
return wheels[index];
}
set
{
wheels[index] = value;
}
}
public int this [string wheel]
{
get
{
for(int i = 0; i < wheels.Length; ++i)
{
if(wheels[i] == wheel) return i;
}
return -1;
}
}
}
class Class_03_22
{
public static void Main(String[] args)
{
Car car1 = new Car();
Console.WriteLine("car1的右前轮是第{0}个轮子", car1["右前轮"]);
Console.WriteLine("car1的左后轮是第{0}个轮子", car1["左后轮"]);
}
}
}
using System;
namespace _03_22
{
class Car
{
string[] wheels = new string[4];
public Car()
{
wheels[0] = "左前轮";
wheels[1] = "右前轮";
wheels[2] = "左后轮";
wheels[3] = "右后轮";
}
public string this [int index] // Car类的索引器
{
get
{
return wheels[index];
}
set
{
wheels[index] = value;
}
}
public int this [string wheel]
{
get
{
for(int i = 0; i < wheels.Length; ++i)
{
if(wheels[i] == wheel) return i;
}
return -1;
}
}
}
class Class_03_22
{
public static void Main(String[] args)
{
Car car1 = new Car();
Console.WriteLine("car1的右前轮是第{0}个轮子", car1["右前轮"]);
Console.WriteLine("car1的左后轮是第{0}个轮子", car1["左后轮"]);
}
}
}