⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 GOF23种设计模式详细例子!附有详细的代码噢!
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace IteratorExample
{
    class Program
    {
        static void Main(string[] args)
        {
            NewsSystem ns = new NewsSystem();
            foreach (NewsEntity news in ns.ShowAllNews(1))
                Console.WriteLine(news.ToString());
            foreach (NewsEntity news in ns.ShowPagedNews(1, 2, 1))
                Console.WriteLine(news.ToString());
        }
    }

    class NewsSystem
    {
        List<NewsEntity> newsData = new List<NewsEntity>();
        List<NewsListEntity> listData = new List<NewsListEntity>();

        public NewsSystem()
        {
            newsData.Add(new NewsEntity(1, "eee", 1));
            newsData.Add(new NewsEntity(2, "ddd", 1));
            newsData.Add(new NewsEntity(3, "ccc", 2));
            newsData.Add(new NewsEntity(4, "bbb", 2));
            newsData.Add(new NewsEntity(5, "aaa", 3));
            listData.Add(new NewsListEntity(1, 1));
            listData.Add(new NewsListEntity(1, 2));
            listData.Add(new NewsListEntity(2, 2));
            listData.Add(new NewsListEntity(2, 2));
        }

        public IEnumerable<NewsEntity> ShowPagedNews(int listID, int pageSize, int pageIndex)
        {
            Console.WriteLine(string.Format(">>>>>>>>>>>>>>>>>>>>ShowPagedNews,pageSize:{0},pageIndex:{1}", pageSize, pageIndex));
            for (int i = pageIndex * pageSize; i < newsData.Count; i++)
            {
                if (GetCategoryList(listID).Contains(newsData[i].CategoryID))
                    if (i - pageIndex * pageSize < pageSize)
                        yield return newsData[i];
                    else
                        yield break;
            }
        }

        public IEnumerable<NewsEntity> ShowAllNews(int listID)
        {
            Console.WriteLine(">>>>>>>>>>>>>>>>>>>>fgShowAllNews");
            for (int i = 0; i < newsData.Count; i++)
            {
                if (GetCategoryList(listID).Contains(newsData[i].CategoryID))
                    yield return newsData[i];
            }
        }

        private List<int> GetCategoryList(int listID)
        {
            List<int> categoryList = new List<int>();
            foreach (NewsListEntity nle in listData)
            {
                if (nle.ListID == listID)
                    categoryList.Add(nle.CategoryID);
            }
            return categoryList;
        }
    }

    class NewsEntity
    {
        private int newsID;

        public int NewsID
        {
            get { return newsID; }
            set { newsID = value; }
        }

        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private int categoryID;

        public int CategoryID
        {
            get { return categoryID; }
            set { categoryID = value; }
        }

        public NewsEntity(int newsID, string title, int categoryID)
        {
            this.newsID = newsID;
            this.title = title;
            this.categoryID = categoryID;
        }

        public override string ToString()
        {
            return string.Format("ID:{0}\tTitle:{1}\tCategoryID:{2}", newsID, title, categoryID);
        }
    }

    class NewsListEntity
    {
        private int listID;

        public int ListID
        {
            get { return listID; }
            set { listID = value; }
        }

        private int categoryID;

        public int CategoryID
        {
            get { return categoryID; }
            set { categoryID = value; }
        }

        public NewsListEntity(int listID, int categoryID)
        {
            this.listID = listID;
            this.categoryID = categoryID;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -