您的位置:首页 > 其它

学习面向对象思想

2013-05-29 09:16 288 查看
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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: