当项是对象时,访问字典中的数据

本文关键字:字典 数据 访问 对象 | 更新日期: 2023-09-27 18:16:45

因此,一旦将数据添加到字典中,我就会丢失如何将数据发送回对象。

用这个数据结构,我做了http://pastebin.com/HicZMzAt完整的代码

public class Computer
{
    public Computer() { }
    public Computer(int _year)
    {
        dropOffDate = DateTime.Now;
        RepairFinished = false;
        Year = _year;
    }

    private DateTime dropOffDate;
    public bool RepairFinished;
    private readonly int Year;
    public static string Plate;
    private string make;
    public string Make
    {
        get { return make; }
        set { make = value; }
    }
    public string Model { get; set; }
    public string ComputerTicketId { get; set; }
    public bool IsLaptop { get; set; }
    public Location Location { get; set; }
    public int HoursWorked { get; set; }
    public double PartsCost { get; set; }
    public DateTime DateFinished { get; set; }
    // public virtual double TotalCost { get { TotalCost = (this.HoursWorked * 50) + PartsCost; } set; }

    public void ComputerPickUp()
    {
        Console.WriteLine("Cost is {0:C} ", this.HoursWorked);
        RepairFinished = true;
    }

,其中我想计算每次系统掉落的不同维修成本。

public class Laptop : Computer
{
    public bool HasCharger { get; set; }
    public Laptop(int year, bool _HasCharger)
        : base(year)
    {
        HasCharger = _HasCharger;
    }
    //TODO overide for COST ! + 10

和我也有一个桌面系统的维修成本更便宜。

但是我用的是

public static class Repair
{
    public static Dictionary<string, object> RepairLog { get; set; }
}

跟踪修理情况现在我迷失在程序的UI部分,无法获取数据来计算定价。

public class RepairUI
   { 
....edited
  Repair.RepairLog = new Dictionary<string, object>();
 ....
 Computer = new Desktop(ComputerYear, HasLcd);

这就是我如何丢失处理数据的方式,每个修复单元(桌面/NBK)的类数据被组织在字典中,现在我想获得数据并编辑对象的修复成本,但我似乎不知道如何到达对象。

那么我如何在取货时询问工作时数并计算该单元的信息呢?

当项是对象时,访问字典中的数据

这听起来是使用界面的好时机!

public Interface IRepairable
{
    double GetRepairCost();
}

然后重新定义Computer

public class Computer : IRepairable
{
    public double GetRepairCost()
    {
        return (this.HoursWorked * 50) + PartsCost;
    }
}

和笔记本

public class Laptop : Computer
{
    public new double GetRepairCost()
    {
        return base.GetRepairCost() + 10;
    }
}

和修复

public static class Repair
{
    public static Dictionary<string, IRepairable> RepairLog { get; set; }
}

现在你有了一个可以调用GetRepairCost()的东西的字典!这些可以是电脑或笔记本电脑或混合,这对维修日志无关紧要!