【问题标题】:Access the data in the dictionary when the item is an object?当项目是对象时访问字典中的数据?
【发布时间】:2015-10-08 20:02:51
【问题描述】:

所以我不知道一旦将数据添加到字典中后如何将数据发送回对象。

使用我为完整代码制作的 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)的类数据都在字典中组织,现在我想获取数据并编辑维修成本object ,但我似乎无法弄清楚如何到达该对象。

那么,我如何在接机时询问并计算单位的信息?

【问题讨论】:

  • 似乎修复类应该计算出来并将其放入一个属性中。 RepairLog 是单机维修的日志吗?

标签: c# oop data-structures


【解决方案1】:

这听起来是使用接口的好时机!

public Interface IRepairable
{
    double GetRepairCost();
}

然后重新定义计算机

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() 的字典!这些可能是计算机或笔记本电脑或混合使用,与 RepairLog 无关!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多