简体   繁体   English

为什么基类中的列表成员被更改?

[英]Why are members of the list in the base class being changed?

This is a simplified example to illustrate my question. 这是一个简化的例子来说明我的问题。 I'll explain after this code. 我将在这段代码之后进行解释。

class Payment
{
    private double amount = 1;
    private DateTime date = new DateTime(2000, 01, 01);
    public Payment() {}
    public double Amount { get { return amount; } 
        set { this.amount = value; } }
    public DateTime Date { get { return date; } }
}

class PaymentStream
{
    public List<Payment> paymentStream = new List<Payment>();
    public PaymentStream()
    {
        for (int i = 0; i < 1; i++)
            paymentStream.Add(new Payment());
    }
    public Payment getIndividualPayment(int index) 
    { return paymentStream[index]; }

    public void displayStream()
    {
        string displayDetails = "Amount {0:c}   Date{1,10:d} ";
        foreach (Payment i in paymentStream)
        {
            Console.WriteLine(displayDetails, i.Amount, i.Date);
        }
    }
}
class DoubledPaymentStream : PaymentStream
{
    public List<Payment> doubledPaymentStream = new List<Payment>();
    public DoubledPaymentStream () : base()
    {
        for (int i = 0; i < 1; i++)
        {
            doubledPaymentStream.Add(getIndividualPayment(i));
            doubledPaymentStream[i].Amount = 
                doubledPaymentStream[i].Amount * 2;
        }
    }
    public Payment getIndividualDoubledPayment(int index)
    { return doubledPaymentStream[index]; }
}
class Program
{
    static void Main(string[] args)
    {
        DoubledPaymentStream doubledstream = new DoubledPaymentStream();
        Console.WriteLine(doubledstream.paymentStream[0].Amount);
        Console.WriteLine(doubledstream.doubledPaymentStream[0].Amount);
    }
}

So its pretty basic. 所以它很基本。 I've created: 我创建了:

  • A class Payment to hold a double and a DateTime. 持有double和DateTime的Payment类。
  • A class PaymentStream that contains a list of Payments. 包含Payments列表的PaymentStream类。
  • A class DoubledPaymentStream that inherits from PaymentStream. 从PaymentStream继承的DoubledPaymentStream类。

In my mind when I create a DoublePaymentStream object I will have two lists: paymentStream and doubledPaymentStream. 在我的脑海中,当我创建DoublePaymentStream对象时,将有两个列表:paymentStream和doubledPaymentStream。 Each list should be independent of the other. 每个列表应相互独立。 The amounts of doubledPaymentStream should be twice the value of paymentStream. doubledPaymentStream的数量应为paymentStream的两倍。

What's happening though is the values in paymentStream are being doubled when they shouldn't. 不过,正在发生的事情是在不应该将PaymentStream中的值加倍的情况下。 So the values in paymentStream end being the same as doubledPaymentStream. 因此,paymentStream中的值与doubledPaymentStream相同。

I think it probably has something to do with DoubledPaymentStreams constructor that calls a method in PaymentStream and somehow that is being reassigned. 我认为这可能与DoubledPaymentStreams构造函数有关,该构造函数调用PaymentStream中的方法,并以某种方式进行重新分配。

Can you please explain what I've done incorrectly and what I need to change to fix this? 您能否解释一下我做错了什么以及需要进行哪些更改以解决此问题?

Thank You! 谢谢!

That would be because when you do: 那是因为当您这样做时:

doubledPaymentStream.Add(getIndividualPayment(i));

You're actually adding the original Payment object to another list. 您实际上是将原始的Payment对象添加到另一个列表中。 Both lists now have a reference to the same object, and when you modify it, it changes in both lists. 现在,两个列表都引用了同一对象,并且在您对其进行修改时,两个列表中的对象都会更改。

You either need to create separate Payment objects to put in your new list, or make Payment a value type . 您需要创建单独的Payment对象以放入新列表中,或将Payment设为值类型

I don't have to explain you the underlying problem, if you read about Value Types and Reference Types you'll come to know you're basically modifying the same reference and hence both the lists are updated. 我不必向您解释潜在的问题,如果您阅读有关值类型和引用类型的信息 ,就会知道您基本上是在修改相同的引用,因此这两个列表都已更新。

So, now we know what's the problem. 所以,现在我们知道了问题所在。 How to fix it? 如何解决? Just make the class as struct or add a copy of instance to doubledPaymentStream . 只需将类设为struct或将实例的副本添加到doubledPaymentStream That should solve your problem. 那应该解决您的问题。

Proably your are using the same class istance and class intance are passed as references in c# 也许您正在使用相同的类istance,并且将类intance作为引用传递给c#

so if you for example 所以如果你举个例子

Payment p = new Payment();
p.amount = 10;
paymentStream.add(p);

and then 接着

p.amount = 2*p.amount;
doubledPaymentStream.add(p); 

both list will contain the same payment with ammount 20. 这两个清单将包含金额为20的相同付款。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM