学习面向对象思想

static void Main(string[] args)
        {
            List<Computer> list = GetComputer();
            Console.WriteLine("请输入要购买电脑的信息 如:品牌、型号、价位、颜色");
            string writeInfo = Console.ReadLine();
            List<Computer> showComputer = new List<Computer>();
            foreach (Computer item in list)
            {
                if (writeInfo == item.Brand) { showComputer.Add(item); }
                if (writeInfo == item.Model) { showComputer.Add(item); }
                if (writeInfo ==item.Price.ToString()) { showComputer.Add(item); }
                if (writeInfo == item.Color) { showComputer.Add(item); }
            }
            Console.WriteLine("符合您要求的电脑:");
            foreach (Computer item in showComputer)
            {
                Console.WriteLine(item.showComputer());
            }


            Console.ReadKey();
        }


        private static List<Computer> GetComputer()
        {
            List<Computer> list = new List<Computer>();
            Computer comasus=new Computer("ASUE","Y系列",4200,"蓝色");
            Computer comlenovo=new Computer("Lenovo","Y系列",6000,"黑色");
            Computer comlenovo1=new Computer("Lenovo","E系列",4500,"黑色");
            Computer comarce=new Computer("Arce","N系列",4000,"紫色");
            list.Add(comasus);
            list.Add(comlenovo);
            list.Add(comlenovo1);
            list.Add(comasus);
            return list;

        }

 

Computer这是新建了一个类库里的一个Computer类

 public class Computer     {         public string Brand { set; get; }         public string Model { set; get; }         public double Price { set; get; }         public string Color { set; get; }
        public Computer() { }         public Computer(string brand, string model, double price, string color)         {             Brand = brand; Model = model; Price = price; Color = color;         }
        public string showComputer()         {             return string.Format("电脑品牌:{0};系列:{1};价格:{2};颜色:{3}",Brand,Model,Price,Color);                    }
    }

posted @   奇奇博客  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示