繁体   English   中英

在克隆对象时需要一些逻辑上的帮助

[英]Need some assistance with my logic in cloning an object

在发布此文章之前,我阅读了之前的一些文章,但我的逻辑确实没有发现任何问题。 (我已经花了3个小时了,这可能会扼杀我的快乐时光) * 我从不知道答案,我很努力地工作,所以也许有人可以问我一个关于我要实现这一目标的问题可以让我根据您的线索或提示来思考答案。 将不胜感激。 * obj2没有克隆,因此在异常stackTrace后面,我发现同一行上存在一个nullpointer异常,这意味着obj2永远不会被克隆。 请帮助我多加思考。

package testbankaccount;

/**
 *
 * @author 
 */
public interface Cloneable {

}

我的父母班

package testbankaccount;

/**
 *
 * @author 
 */
public abstract class BankAccount implements Cloneable, Comparable {
    private double balance; 
    private int numberofDeposits; 
    private int numberofWithdrawals; 
    private double annualInterestRate; 
    private double monthlyServiceCharges; 
    private String customerName;



protected BankAccount(){
    this(1.0, 1.0);
}

protected BankAccount(double balance, double annualInterestRate){
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
}

public void deposit(double deposit){
    balance += deposit;
    numberofDeposits++; 
}

public void withdraw(double withdrawal){
    balance -= withdrawal;
    numberofWithdrawals++;
}

public void calcInterest(){
    double monthlyInterestRate = annualInterestRate/1200;
    double monthlyInterest = balance * monthlyInterestRate;
    balance += monthlyInterest; 
}

public void setMonthlyServiceCharge(double serviceCharge){
    monthlyServiceCharges = serviceCharge;
}

public void monthlyProcess(){
    balance -= monthlyServiceCharges;
    calcInterest();
    numberofWithdrawals = 0;
    numberofDeposits = 0;
    monthlyServiceCharges = 0.0;
}
public void setBalance(double balance){
     this.balance = balance;
}

public double getBalance(){
    return balance;
}

public int getNumberWithdraws(){
    return numberofWithdrawals;
}

@Override
    public Object clone() throws CloneNotSupportedException {

        return super.clone();

      }



    @Override
public abstract String toString();

    @Override
public abstract boolean equals(Object obj);

}

我的子类

package testbankaccount;

/**
 *
 * @author Darren Wright
 */
public class SavingsAccount extends BankAccount implements Cloneable, Comparable {

private boolean status;


public SavingsAccount(){
    this(1.0,1.0);
}

public SavingsAccount(double balance, double annualInterestRate){
    super(balance,annualInterestRate);

}

public void setActivity(){
    if (getBalance() > 25.0)
     status = true;
    else status = false;
}

    @Override
public void withdraw(double withdrawal){
    if (status = true)
    super.withdraw(withdrawal);     
}

@Override
public void deposit(double deposit){
    if (status = false && (deposit + getBalance()) > 25.0)
    {
            status = true;
            super.deposit(deposit);
    }
    else if (status = true)
            super.deposit(deposit);
}

    @Override
public void monthlyProcess(){
        double result = 0.0;
        if(getNumberWithdraws() >4)
        result = getNumberWithdraws() - 4;
        setMonthlyServiceCharge(result);
        setActivity();
}
@Override
    public String toString() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

@Override
    public Object clone() throws CloneNotSupportedException {

        return super.clone();

      }

    @Override
    public boolean equals(Object obj) {
        return (getBalance() == ((SavingsAccount)obj).getBalance());
    }


  public int compareTo(Object obj) {
    if (getBalance() > ((SavingsAccount)obj).getBalance())
      return 1;
    else if (getBalance() < ((SavingsAccount)obj).getBalance())
      return -1;
    else
      return 0;
  }


}

我的考试课

package testbankaccount;

/**
 *
 * @author 
 */
public class TestBankAccount{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  throws CloneNotSupportedException {
        SavingsAccount obj1 = new SavingsAccount(500,8.25); 
        try{
            SavingsAccount obj2 = (SavingsAccount)obj1.clone();
            System.out.println(obj1.compareTo(obj2));
            System.out.println(obj1.equals(obj2));
         }
         catch (CloneNotSupportedException ex) {
             ex.printStackTrace();
        }

    }
}

我的错误输出

java.lang.CloneNotSupportedException: testbankaccount.SavingsAccount
    at java.lang.Object.clone(Native Method)
    at testbankaccount.BankAccount.clone(BankAccount.java:69)
    at testbankaccount.SavingsAccount.clone(SavingsAccount.java:60)
    at testbankaccount.TestBankAccount.main(TestBankAccount.java:16)
BUILD SUCCESSFUL (total time: 0 seconds)

我在思考过程中缺少什么? 我创建了接口,实现了它,并在我的父类和子类中对其进行了覆盖。 我的子类将super.clone()引用到超类,而我正在考虑的超类中的super.clone()引用对象的clone方法。 我在测试类中正确地进行了转换,但是obj2在compareTo和equals中都为null。 我在想什么呢

您不应该创建自己的Cloneable接口。 您应该使用内置的

不要创建自己的公共接口Cloneable。 现在您得到的是您定义的,而不是系统的。 因此,不是实现“真正的” Cloneable,而是实现自己的对象,Object.clone函数然后将其识别为无法克隆对象。

您还需要为每个您希望能够克隆的类编写一个公共克隆函数,以覆盖从私有到公共的保护。

由于我不了解的原因,Java使使对象可克隆成为了一个主要的痛苦。

暂无
暂无

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

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