我编写了一个模拟股票的类:
public Stock(String ticker, int shares, float purchasePrice) throws IllegalArgumentException
{
if(ticker == null || ticker == "" ||
shares <= 0 || purchasePrice <= 0)
throw new IllegalArgumentException(Messages.enterAppropriateValues());
this.ticker = ticker;
this.shares = shares;
this.purchasePrice = purchasePrice;
latestPrice = purchasePrice;
updatePercentGain();
}
复制构造函数如下所示:
public Stock(Stock other) throws IllegalArgumentException
{
this(other.ticker, other.shares, other.purchasePrice);
}
以下是我用来测试的命令:
Stock bac = new Stock("BAC", 100, 42.22f);
System.out.println(bac);
bac.setLatestPrice(43.35f);
System.out.println(bac);
Stock bacCopy = new Stock(bac);
System.out.println(bacCopy);
输出是:
BAC 100 42.22 42.22 0.00%
BAC 100 42.22 43.35 2.68%
BAC 100 42.22 42.22 0.00%
出于某种原因,代表增益百分比的最后一个值没有被复制?
这是百分比增益方法顺便说一句:
public void updatePercentGain()
{
percentGain = ((latestPrice - purchasePrice) / purchasePrice) * 100;
}
我哪里错了?