C#基础:编写一个类立文体,让其实现IEnumarable接口,细节是令其可以遍历迭代长宽高...

C#基础:编写一个类立文体,让其实现IEnumarable接口,细节是令其可以遍历迭代长宽高
今天去上课!又学到不少东西!
晚上加班把学到的一点东西,与大家分享一下!




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Cub cub = new Cub(1, 2, 3);
            foreach (int vorter in cub)     //foerach循环结构!
            {
                Console.WriteLine(vorter);
           
            }
        }
    }
    public class Cub : IEnumerable  //继承IEnumerable, IEnumarable接口
    {
        int Length;
        int Wicth;
        int Herht;  //定义三个int类型的变量!

        public Cub(int length, int Wicth, int Herht)
        {
            this.Length = length;
            this.Wicth = Wicth;
            this.Herht = Herht;
        }

        #region IEnumerable 成员

        public IEnumerator GetEnumerator()
        {
            return new CubEnumera(this);
        }

        #endregion

        public int this[uint i]     //索引!
        {
            get {
                switch (i)
                {
                    case 0: return Length;
                    case 1: return Wicth;
                    case 2: return Herht;
                    default: throw new IndexOutOfRangeException(
                     "Attempt to retrieve Vector element" + i);
                }
            }
            set
            {
                switch (i)
                {
                    case 0: Length = value; break;
                    case 1: Wicth = value; break;
                    case 2: Herht = value; break;
                    default:
                        throw new IndexOutOfRangeException(
                           "Attempt to set Vector element" + i);
               
                }
           
            }
       
        }
    }

    public class CubEnumera : IEnumerator
    {
        Cub cub;
        int num;

        public CubEnumera(Cub cub)
        {
            this.cub = cub;
            num = -1;
        }

        #region IEnumerator 成员

        public object Current
        {
            get {
                if(num < 0 || num > 2)
               
                throw new NotImplementedException("");
                return cub[(uint)num];
            }
        }
       
        public bool MoveNext()
        {
            ++num;
            return (num > 2) ? false:true ;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

   
}



后记:

      刚写完上面代码!虽然知道怎么写了!但还不知道应该怎么用!
      注释也不应该怎么去写!
      希望大家明白的能指点一下!
      感谢了!
      先睡觉了!
      明天还早班!     

转载于:https://www.cnblogs.com/sunfei_131121/archive/2009/10/12/1581126.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值