[作业10-11]1.编写一个类立方体Cub,让其实现IEnumarable接口,细节是令其可以遍历迭代长宽高,并做一个客户代码验证。
2009-10-12 15:35
926 查看
Cub.cs代码:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication1
{
public class Cub : IEnumerable //声明立方体Cub类继承IEnumerable接口!
{
int length; //声明长度!
int width; //声明宽度!
int height; //声明高度!
public void cub(int length, int width, int height) //声明构造函数,并用长宽高签名!
{
this.length = length;
this.width = width;
this.height = height;
}
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
return new CubEnumerator(this); //返回当前枚举对象!
}
#endregion
public int this[uint i] //索引器!
{
get
{
switch (i)
{
case 0:
return length;
case 1:
return width;
case 2:
return height;
default:
throw new IndexOutOfRangeException(
"Attempt to retrieve Vector element" + i);
}
}
set
{
switch (i)
{
case 0:
length = value;
break;
case 1:
width = value;
break;
case 2:
height = value;
break;
default:
throw new IndexOutOfRangeException(
"Attempt to set Vector element" + i);
}
}
}
}
#region enumerator class
public class CubEnumerator : IEnumerator //声明CubEnumerator类,并继承接口IEnumerator!
{
Cub theCub; // Cub object that this enumerato refers to
int location; // which element of theCub the enumerator is currently referring to
public CubEnumerator(Cub theCub)
{
this.theCub = theCub;
location = -1;
}
public bool MoveNext()
{
++location;
return (location > 2) ? false : true;
}
public object Current
{
get
{
if (location < 0 || location > 2)
throw new InvalidOperationException(
"The enumerator is either before the first element or " +
"after the last element of the Vector");
return theCub[(uint)location];
}
}
public void Reset()
{
location = -1;
}
}
#endregion
}
Program.cs代码:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Cub c = new Cub(); //实例化一个Cub类!
c.cub(2, 3, 4); //初始化长、宽、高的值!
foreach (int temp in c) //迭代集合c!
{
foreach (int next in c) //迭代集合c!
{
Console.Write(" " + next);
}
Console.WriteLine(temp);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication1
{
public class Cub : IEnumerable //声明立方体Cub类继承IEnumerable接口!
{
int length; //声明长度!
int width; //声明宽度!
int height; //声明高度!
public void cub(int length, int width, int height) //声明构造函数,并用长宽高签名!
{
this.length = length;
this.width = width;
this.height = height;
}
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
return new CubEnumerator(this); //返回当前枚举对象!
}
#endregion
public int this[uint i] //索引器!
{
get
{
switch (i)
{
case 0:
return length;
case 1:
return width;
case 2:
return height;
default:
throw new IndexOutOfRangeException(
"Attempt to retrieve Vector element" + i);
}
}
set
{
switch (i)
{
case 0:
length = value;
break;
case 1:
width = value;
break;
case 2:
height = value;
break;
default:
throw new IndexOutOfRangeException(
"Attempt to set Vector element" + i);
}
}
}
}
#region enumerator class
public class CubEnumerator : IEnumerator //声明CubEnumerator类,并继承接口IEnumerator!
{
Cub theCub; // Cub object that this enumerato refers to
int location; // which element of theCub the enumerator is currently referring to
public CubEnumerator(Cub theCub)
{
this.theCub = theCub;
location = -1;
}
public bool MoveNext()
{
++location;
return (location > 2) ? false : true;
}
public object Current
{
get
{
if (location < 0 || location > 2)
throw new InvalidOperationException(
"The enumerator is either before the first element or " +
"after the last element of the Vector");
return theCub[(uint)location];
}
}
public void Reset()
{
location = -1;
}
}
#endregion
}
Program.cs代码:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Cub c = new Cub(); //实例化一个Cub类!
c.cub(2, 3, 4); //初始化长、宽、高的值!
foreach (int temp in c) //迭代集合c!
{
foreach (int next in c) //迭代集合c!
{
Console.Write(" " + next);
}
Console.WriteLine(temp);
}
}
}
}
相关文章推荐
- implementors一个eclipse面向接口编程的非常有用的插件:可以直接跳到接口的实现代码
- java实现网上在线支付--09,10,11,12_分析易宝支付网关的应答协议与处理代码,完成用于处理支付响应的Servlet的初步编写和调试,完成处理支付网关响应结果的Servlet,支付实现
- 习题10:参照Windows系统“附件”中的“计算器”,自行编写一个简易的计算器。要求:可以实现由0~4构成的整数的加减运算。
- java在线支付---09,10,11,12_在线支付_分析易宝支付网关的应答协议与处理代码,完成用于处理支付响应的Servlet的初步编写和调试,完成处理支付网关响应结果的Servlet,支付实现
- 10_9_1编写代码,定义一个基类MyClass,其中包括虚礼方法GetString(),这个方法返回存储在受保护字段myString中的字符串,该字段可以通过只写公共属性ContainedStrin
- 重构 — 改善既有的类图设计 条款10:将多个接口的实现类合并成一个
- 抽象类实现一个接口为什么可以不用都实现接口中的方法
- 10_9_5为Ch10CardLib库编写一个控制台客户程序,从扰乱的Deck对象中一次取出5张牌.如果这5张牌都是相同的花色,客户程序就应在屏幕上显示这5张牌,以及文本"Flush",否则就显示50
- 定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下: (1)可以生成具有特定坐标的点对象。 (2)提供可以设置三个坐标的方法。 (3)提供可以计算该“点”距原点距离平方的方法。 (4)编写主类程序验证。
- //1.编写代码模拟三次密码输入的场景。//2.编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
- 【Step by Step】编写代码验证一个ASP.NET应用程序和页面的生命周期
- 编写高质量代码改善C#程序的157个建议——建议10: 创建对象时需要考虑是否实现比较器
- Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么?
- 10_9_2从类MyClass中派生一个类MyDervedClass,重写GetString(),使用该方法的基类实现代码从基类中返回一个字符串,但在返回的字符串中添加文本"(output fro)"
- Python实现日志自动记录功能(一个思路代码,详细可以自己扩展)
- 一个 11 行 Python 代码实现的神经网络
- 编写高质量OC代码52建议总结:9.以“族类模式“隐藏实现细节
- java中如何在eclipse里查看一个接口的所有实现类或一个类的子父级关系?
- Factory 定义一个接口,客户可以使用这个接口创建一个对象.同时,我们还可以控制对那个类进行实例化
- 最近做了一个小东西,对于接口实现类。发现很多都是一个套路,所以备份下,以后可以经常用到! 这几个是单纯查询的实现类。