复制私有数据成员

时间:2016-11-29 02:34:59

标签: java variables copy setter deep-copy

我正在研究一个程序:

创建一个Java Stock类来表示有关买入和卖出的股票的信息....每当股票价格发生变化时,每股价格的变化也会更新。省略设定者以改变每股价格。每股价格的变化仅在每股价格变化时设定。不要在构造函数中包含用于更改价格的参数。将更改初始化为零。

我认为可以计算股价变化的唯一方法是创建第四个变量(originalPrice),将其等同于sharePrice,然后减去它们。但是,这不起作用,我不知道如何创建一个" deep"复制变量。有没有办法让originalPrice成为sharePrice的副本,然后可以从sharePrice单独更新?谢谢您的帮助!

股票类:

public class Stock {
    private String name;
    private double sharePrice;
    private double originalPrice;
    private double changeSharePrice = 0;

    /**
     * @param name
     * @param sharePrice
     * @param changeSharePrice
     */
    public Stock(String stockName, double sharePrice) {
        setName(stockName);
        setSharePrice(sharePrice);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String stockName) {
        this.name = stockName;
    }

    /**
     * @return the sharePrice
     */
    public double getSharePrice() {
        return sharePrice;
    }

    /**
     * @param sharePrice the sharePrice to set
     */
    public void setSharePrice(double sharePrice) {
        this.sharePrice = sharePrice;
        originalPrice = sharePrice;
        changeSharePrice = sharePrice - originalPrice;
    }

    /**
     * @return the changeSharePrice
     */
    public double getChangeSharePrice() {
        return changeSharePrice;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Stock Name: " + name + "/n Price Per Share: " + sharePrice + "/n Change in Price Per Share: "
            + changeSharePrice;
    }

    /*
     * Copy Constructor
     */
    public Stock(Stock other) {
        name = other.name;
        sharePrice = other.sharePrice;
        changeSharePrice = other.changeSharePrice;
    }

    /*
     * Deep copy method
     */
    public Stock copy() {
        return new Stock(name, sharePrice);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object) Equals Method
     */
    public boolean equals(Object obj) {
        Stock other = (Stock) obj;
        if (name == other.name)
            return true;
        else
            return false;
    }
}

这是我的主要内容:

public class tester {

    public static void main(String[] args) {
        // An object is created for Nomad Foods Limited
        Stock one = new Stock("NOMD", 10.46);
        //An object is created for Treehouse Foods
        Stock two = new Stock("THS", 69.18);        
        one.setSharePrice(4.00);
        System.out.print(one.getChangeSharePrice());
    }
}

2 个答案:

答案 0 :(得分:0)

您的整体逻辑看起来很合理,但您遇到了问题:

public void setSharePrice(double sharePrice) {
    this.sharePrice = sharePrice;
    originalPrice = sharePrice;
    changeSharePrice = sharePrice - originalPrice;
}

您将originalPrice设置为等于sharePrice然后取两者的差异。这当然是零。

如果您重新订购陈述,您应该得到您想要的结果:

public void setSharePrice(double sharePrice) {
    changeSharePrice = sharePrice - this.sharePrice;
    this.sharePrice = sharePrice;
}

这甚至可以让您取消originalPrice

PS:它与您的主要问题无关,但您的equals()方法存在错误。请勿使用==来比较字符串,而是使用String.equals()。因此name == other.name应为name.equals(other.name)

答案 1 :(得分:0)

将构造函数更改为

public Stock(String stockName, double sharePrice) {
    setName(stockName);
    this.sharePrice = sharePrice;
    this.originalPrice = sharePrice;

}

假设您希望changeSharePrice值与原始值相比更改,请将您的setter更改为

public void setSharePrice(double sharePrice) {
    this.sharePrice = sharePrice;
    changeSharePrice = sharePrice - originalPrice;

}