using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace MyComputer
{ public class Computer { /// <summary> /// 计算机型号 /// </summary> public string Type { get; set; } /// <summary> /// 计算机ID /// </summary> public string ID { get; set; } /// <summary> /// 购买日期 /// </summary> public string BuyDate { get; set; } } } |
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace MyComputer
{ public class Manager { /// <summary> /// 设置计算机ID /// </summary> /// <param name="comps"></param> public void SetID(Computer[] comps) { Random rand = new Random();
foreach (Computer cp in comps)
{ cp.ID = cp.Type + "-" + rand.Next(1000, 9999); } } } } |
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace MyComputer
{ class Program { static void Main(string[] args) { Manager myManager = new Manager();
//初始化计算机信息
#region Init Computers Computer[] computers = new Computer[3]{ new Computer(), new Computer(), new Computer() }; computers[0].Type = "hpCQ-217TX"; computers[0].BuyDate = "2013-10-5"; computers[1].Type = "Mc240CH/A"; computers[1].BuyDate = "2013-10-5"; computers[2].Type = "SYNW18H/W"; computers[2].BuyDate = "2013-10-5"; #endregion
//设置计算机ID之前的计算机信息
Console.WriteLine("***************设置计算机ID前******************"); Console.WriteLine("计算机型号\t计算机ID \t购买时间"); foreach (Computer item in computers) { Console.WriteLine("{0}\t{1}\t {2}\t", item.Type, item.ID, item.BuyDate);
}
Console.WriteLine("\n***************设置计算机ID后******************");
//设置计算机ID myManager.SetID(computers); //设置计算机ID之后的计算机信息 Console.WriteLine("计算机型号\t计算机ID \t购买时间"); foreach (Computer item in computers) { Console.WriteLine("{0}\t{1}\t{2}\t", item.Type, item.ID, item.BuyDate);
}
Console.ReadLine(); } } } |
深入.net和C#编程第二章上机练习2
最新推荐文章于 2023-05-27 21:00:03 发布