1

这个想法很简单。为商店的“部门”创建一个结构,给它一个用于命名的变量(一个名为“部门”的字符串),以及一个用于保存在该部门完成的所有购买的数组。

现在,我希望每次在特定部门保存购买时,它都会根据部门名称和购买金额自动应用折扣。

现在,示例类:

class Program
{
    struct Departments
    {
        public string Department;
        private double[] _buys;

        public double[] Buys
        {
            get { return _buys; }
            set
            {
                if (value > 100)
                {
                    if (Department == "CLOTH")
                    _buys = value * .95;
                    if (Department == "FOOD")
                    _buys = value * .90;
                    if (Department == "OTHER")
                    _buys = value * .97;
                }
                _buys = value;
            }
        }
    }

    static void Main()
    {
        var departments = new Departments[3];
        departments[0].Department = "CLOTH";
        departments[1].Department = "FOOD";
        departments[2].Department = "OTHER";
        departments[0].Buys = new double[5];
        departments[0].Buys[0] = 105;
    }
}

注意这一行departments[0].Buys[0] = 105,这就是我想保存购买的东西的方式,“代码简单”......

现在,注意Buys结构的属性,它是一个“数组属性”。然后,当我使用value > 100条件时,它给出了一个明显的错误,不能从doubleto 转换double[]

问题......我怎样才能为 写一个正确的条件value > 100,必须在结构上放置什么才能实现这一点?

我已经尝试过使用“索引器”,但只要我尝试过,我就无法让它以departments[0].Buys[0] = 105正确的方式通过分配。

请注意,我想保留这个模式,特别是为了方便简单地说departments[0].Buys[0] = 105购买

编辑:

前面的结构“部门”仅用于示例目的。我不会回答如何通过另一种方式来拥有正确的“部门”,我想要一个关于如何使 set 参数对数组的各个元素起作用的答案

4

6 回答 6

2

另一种可能的解决方案是为 _buys 数组创建另一个类:

class Buys
{
    private double[] _buys;

    public Buys (int capacity)
    {
        _buys = new double[capacity];
    }

    public double this[int index]
    {
        get { return _buys; }
        set 
        {
            if (value > 100)
            {
                if (Department == "CLOTH")
                    value = value * .95;
                if (Department == "FOOD")
                    value = value * .90;
                if (Department == "OTHER")
                    value = value * .97;
            }
            _buys = value;
        }
    }
}

struct Departments
{
    public string Department;
    public Buys Buys;
}

static void Main()
{
    var departments = new Departments[3];
    departments[0].Department = "CLOTH";
    departments[1].Department = "FOOD";
    departments[2].Department = "OTHER";
    departments[0].Buys = new Buys(5);
    departments[0].Buys[0] = 105;
}
于 2012-04-05T03:21:24.687 回答
1

您最好使用 aList<double>来记录购买情况。这样列表可以动态增长。您还可以使用索引来获取列表元素。

您可以使用字典简化折扣代码。

对于这些数据,您最好使用 a class,而不是 a structStructs 通常更适合用于不可变值。使类代表一个部门并在其中存储适当的折扣。

所以是这样的:

class Program
{
    class Department
    {
        public string Name;
        public double Discount;

        private List<double> _buys = new List<double>();

        public List<double> Buys
        {
            get { return _buys; }
        }

        public void AddBuy(double value)
        {
            _buys.Add(value > 100 ? value * discount : value);
        }
    }

    static void Main()
    {
        var departments = new List<Department>();
        departments.Add(new Department { Name = "CLOTH", Discount = 0.95 });
        departments.Add(new Department { Name = "FOOD", Discount = 0.90 });
        departments.Add(new Department { Name = "OTHER", Discount = 0.97 });
        departments[0].AddBuy(105);

        Console.WriteLine(departments[0].Buys[0]);
    }
}

我还有很多其他方法可以改进这个设计,但这应该能让你继续前进。

于 2012-04-05T03:03:38.300 回答
1

你可以做这样的事情

public class Departments
{
    public string Department;
    public MyList buys;
    public Departments()
    {
        buys = new MyList(this, 5);
    }
}
public class MyList
{
    private double[] backingList;
    private Departments owner;
    public MyList(Departments owner, int size)
    {
        this.owner = owner;
        backingList = new T[size];
    }

    public double this[int index]
    {
        get{ return backingList[index]; }
        set { backingList[index] = discountFor(owner.Department) * value; }
    }

    private float discountFor(string department)
    {
        switch(department)
        {
        case "department1":
            return 0.5f;
        //...
        default:
             return 1.0f;
        }
    }

}

但是,通过将折扣放入设置器自身,您并没有保持良好的关注点分离。更好的代码看起来像

departments[0].Buys[0] = DiscountFor("department1") * 105;
于 2012-04-05T03:31:56.497 回答
0

通过你的错误,你可以做这样的事情。

struct Departments
    {
        public string Department;
        private double[] _buys;
        public double[] Buys
        {
            get { return _buys; }
            set
            {
                for (int i = 0; i < value.Length; i++)
                {
                    if (value[i] > 100)
                    {
                        if (Department == "CLOTH")
                            _buys[i] = value[i] * .95; if (Department == "FOOD")
                            _buys[i] = value[i] * .90; if (Department == "OTHER")
                            _buys[i] = value[i] * .97;
                    }
                }
                _buys = value;
            }
        }
    }
于 2012-04-05T03:05:48.150 回答
0

您可以创建一个方法来执行这样的设置:

        public double[] Buys { get; set; }

        public void SetBuy(int index, double value)
        {
           if (value > 100)
           {
              if (Department == "CLOTH")
               value = value * .95;
              if (Department == "FOOD")
               value = value * .90;
              if (Department == "OTHER")
               value = value * .97;
           }
           _buys[index] = value;
        }
于 2012-04-05T03:11:16.517 回答
0

包含对可变项的可变引用的结构通常是一个坏主意,因为这种结构最终会表现出值和引用语义的奇怪组合。例如,应该是什么效果:

  部门 dep1,dep2;
  ...
  dep1 = dep2;
  dep1.Department = "布料";
  dep1.Buys[5] = 123;

这样的声明将或应该影响dep2.Buys[5]. 如果给定结构的Buys字段/属性总是引用同一个数组,这样的语义可能是可以容忍的,但是如果数组需要调整大小会发生什么?

于 2012-04-07T21:17:17.590 回答