实验5-1
B.
(1)当装入水果超过10个时,会抛出异常:索引超过了数组界限
解决方法:修改程序在加入前比较水果篮中数目是否已经为10,若未达到则继续添加,若水果数量达到十个时,则停止添加,并弹出篮子已满的警告.
修改程序如下:
using System;
namespace test5_1_1
{
public class Fruit
{
public virtual string Name
{
get
{
return ("Fruit");
}
}
public static int length { get; set; }
}
public class Apple : Fruit
{
public override string Name
{
get
{
return ("Apple");
}
}
}
public class Banana : Fruit
{
public override string Name
{
get
{
return ("Banana");
}
}
}
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
object Current
{
get;
}
bool MoveNext();
void Reset();
}
public class FruitBasket : IEnumerable
{
static int Max = 10;
Fruit[] basket = new Fruit[Max];
int a = Fruit.length;
public static int count = 0;
internal Fruit this[int index]
{
get
{
return (basket[index]);
}
set
{
basket[index] = value;
}
}
internal int Count
{
get
{
return (count);
}
}
public void Add(Fruit fruit)
{
if (count >= Max)
{
Console.WriteLine("超出水果筐容量!");
}
basket[count++] = fruit;
}
public IEnumerator GetEnumerator()
{
return (new FruitBasketEnumerator(this));
}
}
public class FruitBasketEnumerator : IEnumerator
{
FruitBasket fruitBasket;
int index;
public void Reset()
{
index = -1;
}
public object Current
{
get
{
return (fruitBasket[index]);
}
}
public bool MoveNext()
{
if (++index >= fruitBasket.Count)
return false;
else
return true;
}
internal FruitBasketEnumerator(FruitBasket fruitBasket)
{
this.fruitBasket = fruitBasket;
Reset();
}
}
class CollectionsExample
{
static void Main(string[] args)
{
int a = 0;
int b = 0;
FruitBasket fruitBasket = new FruitBasket();
for (int i = 0; i < 2; i++)
{
if (FruitBasket.count <= 10)
{
Console.WriteLine("Adding a Banana");
fruitBasket.Add(new Banana());
a = i + 1;
}
else
{
break;
}
}
for (int j = 0; j < 7; j++)
{
if (FruitBasket.count <= 9)
{
Console.WriteLine("Adding an Apple");
fruitBasket.Add(new Apple());
b = j + 1;
}
else
{
break;
}
}
if (a + b == 10)
{
Console.WriteLine("水果篮容量已到达上限!");
}
Console.WriteLine();
Console.WriteLine("The basket is holding!");
Console.WriteLine(a + " Apple(s)");
Console.WriteLine(b + " Banana(s)");
Console.Read();
}
}
}
运行结果如下:
未达到10个时,香蕉两个,苹果七个
总数达到10个时,香蕉四个,苹果六个
(2)在水果篮中加入橙子
实验代码:
using System;
namespace test5_1_2
{
public class Fruit
{
public virtual string Name
{
get
{
return ("Fruit");
}
}
public static int length { get; set; }
}
public class Apple : Fruit
{
public override string Name
{
get
{
return ("Apple");
}
}
}
public class Banana : Fruit
{
public override string Name
{
get
{
return ("Banana");
}
}
}
public class Orange : Fruit
{
public override string Name
{
get
{
return ("Orange");
}
}
}
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
object Current
{
get;
}
bool MoveNext();
void Reset();
}
public class FruitBasket : IEnumerable
{
static int Max = 10;
Fruit[] basket = new Fruit[Max];
int a = Fruit.length;
public static int count = 0;
internal Fruit this[int index]
{
get
{
return (basket[index]);
}
set
{
basket[index] = value;
}
}
internal int Count
{
get
{
return (count);
}
}
public void Add(Fruit fruit)
{
if (count >= Max)
{
Console.WriteLine("超出水果筐容量!");
}
basket[count++] = fruit;
}
public IEnumerator GetEnumerator()
{
return (new FruitBasketEnumerator(this));
}
}
public class FruitBasketEnumerator : IEnumerator
{
FruitBasket fruitBasket;
int index;
public void Reset()
{
index = -1;
}
public object Current
{
get
{
return (fruitBasket[index]);
}
}
public bool MoveNext()
{
if (++index >= fruitBasket.Count)
return false;
else
return true;
}
internal FruitBasketEnumerator(FruitBasket fruitBasket)
{
this.fruitBasket = fruitBasket;
Reset();
}
}
class CollectionsExample
{
static void Main(string[] args)
{
int a = 0;
int b = 0;
int c = 0;
FruitBasket fruitBasket = new FruitBasket();
for (int i = 0; i < 3; i++)
{
if (FruitBasket.count <= 10)
{
Console.WriteLine("Adding a Banana");
fruitBasket.Add(new Banana());
a = i + 1;
}
else
{
break;
}
}
for (int j = 0; j < 3; j++)
{
if (FruitBasket.count <= 9)
{
Console.WriteLine("Adding an Apple");
fruitBasket.Add(new Apple());
b = j + 1;
}
else
{
break;
}
}
for (int k = 0; k < 3; k++)
{
if (FruitBasket.count <= 9)
{
Console.WriteLine("Adding an Oange");
fruitBasket.Add(new Orange());
c = k + 1;
}
else
{
break;
}
}
if (a + b + c == 10)
{
Console.WriteLine("水果篮容量已到达上限!");
}
Console.WriteLine();
Console.WriteLine("The basket is holding!");
Console.WriteLine(a + " Apple(s)");
Console.WriteLine(b + " Banana(s)");
Console.WriteLine(c + " Orange(s)");
Console.Read();
}
}
}
实验结果:
未达到10个,三种水果各三个
超过十个,香蕉三个,苹果两个,橙子六个