List 列表循环 简单的游戏选择器 不使用for、forcach也可遍历

在做某些需要点一下鼠标或者自动跳到列表下一个、上一个时使用,这样就可以不使用 for,foreach 来遍历寻找了

比如:

首先定义一个列表循环类,继承自 List ,这样就具有 List 所有的特性

代码: 

public class ListLoop<T> : List<T>
{

    public int CurrentID { get; private set; }

    public int LastID
    {
        get
        {
            CurrentID--;
            if (CurrentID < 0) CurrentID = this.Count - 1;
            CurrentID %= this.Count;
            return CurrentID;
        }
    }

    public int NextID
    {
        get
        {
            CurrentID++;
            if (CurrentID == this.Count) CurrentID = 0;
            CurrentID %= this.Count;
            return CurrentID;
        }
    }

    public T CurrentValue
    {
        get { return this[CurrentID]; }
    }

    public T LastValue
    {
        get { return this[LastID]; }
    }

    public T NextValue
    {
        get { return this[NextID]; }
    }
}

 

初始化这个类,并添加内容

//初始化
private ListLoop<int> mListLoop = new ListLoop<int>()
{
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14
}; 

调用:

//调用
//当前值
string currentValue = mListLoop.CurrentValue.ToString();
//上一个值
string lastValue = mListLoop.LastValue.ToString();
//下一个值
string nextValue = mListLoop.NextValue.ToString();
//当前ID
string mListCurrentID = "当前ID:" + mListLoop.CurrentID;

 

示例:下面做一个游戏选择器,类都用 string 代替,重要的是方法

首先新建一个类 GameChoose

public class GameChoose
{
    //ID
    public int ID { get; set; }

    //名称
    public string Name { get; set; }

    //显示图片
    public string ShowPic { get; set; }

    //背景图
    public string BackgroundImage { get; set; }

    //简介
    public string Introduction { get; set; }

    //公告
    public string Notice { get; set; }

    //其它
    public string Other { get; set; }

    //特效
    public string SpecialEffects { get; set; }

    public GameChoose(int id, string name)
    {
        this.ID = id;
        this.Name = name;
    }

    public GameChoose(int id, string name, string showPic, string backgroundImage)
    {
        this.ID = id;
        this.Name = name;
        this.ShowPic = showPic;
        this.BackgroundImage = backgroundImage;
    }

    public GameChoose(int id, string name, string showPic, string backgroundImage, string introduction)
    {
        this.ID = id;
        this.Name = name;
        this.ShowPic = showPic;
        this.BackgroundImage = backgroundImage;
        this.Introduction = introduction;
    }

    public GameChoose(int id, string name, string showPic, string backgroundImage, string introduction, string notice)
    {
        this.ID = id;
        this.Name = name;
        this.ShowPic = showPic;
        this.BackgroundImage = backgroundImage;
        this.Introduction = introduction;
        this.Notice = notice;
    }

    public GameChoose(int id, string name, string showPic, string backgroundImage, string introduction, string notice, string specialEffects)
    {
        this.ID = id;
        this.Name = name;
        this.ShowPic = showPic;
        this.BackgroundImage = backgroundImage;
        this.Introduction = introduction;
        this.Notice = notice;
        this.SpecialEffects = specialEffects;
    }
}

 

初始化选择器的内容:

ListLoop<GameChoose> gameChooses = new ListLoop<GameChoose>()
{
    new GameChoose(1,"游戏一"),
    new GameChoose(2,"游戏二","显示二","背景二"),
    new GameChoose(3,"游戏三", "显示三","背景三","简介三"),
    new GameChoose(4,"游戏四", "显示四","背景四","简介四","公告四"),
    new GameChoose(5,"游戏五", "显示五","背景五","简介五","公告五","特效五"),
};

调用:不使用 for,foreach 来遍历

int temp = gameChooses.NextID;

int id = gameChooses.CurrentValue.ID;

string name = gameChooses.CurrentValue.Name;

string showPic = gameChooses.CurrentValue.ShowPic;

string backgroundImage = gameChooses.CurrentValue.BackgroundImage;

string introduction = gameChooses.CurrentValue.Introduction;

string notice = gameChooses.CurrentValue.Notice;

string specialEffects = gameChooses.CurrentValue.SpecialEffects;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值