2

在发布此之前,我阅读了以前的一些帖子,我真的没有看到我的逻辑有什么问题。(我已经花了 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() 是指 Object 的 clone 方法。我在测试类中正确投射,但 obj2 在 compareTo 和 equals 中最终都为 null。我在想什么?

4

2 回答 2

5

您不应该创建自己的Cloneable界面。您应该使用内置的.

于 2012-04-27T17:58:55.517 回答
1

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

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

由于我不明白的原因,Java 使对象可克隆成为一大难题。

于 2012-04-27T18:02:41.053 回答