Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在克隆对象时需要我的逻辑帮助吗_Java_Nullpointerexception_Compareto_Cloneable - Fatal编程技术网

Java 在克隆对象时需要我的逻辑帮助吗

Java 在克隆对象时需要我的逻辑帮助吗,java,nullpointerexception,compareto,cloneable,Java,Nullpointerexception,Compareto,Cloneable,在我发布这篇文章之前,我阅读了之前的一些帖子,我真的没有发现我的逻辑有任何错误。(我已经做了3个小时了,这可能会扼杀我的快乐时光)*我从来不想知道答案,我喜欢努力工作,所以如果有人能问我一个问题,关于我试图实现的目标,这可以引导我利用你的线索或提示来思考答案。非常感谢。*obj2没有克隆,因此在异常stackTrace后面,我发现在同一行上有一个nullpointer异常,这意味着obj2永远不会被克隆。请帮我再仔细想想 package testbankaccount; /** * * @

在我发布这篇文章之前,我阅读了之前的一些帖子,我真的没有发现我的逻辑有任何错误。(我已经做了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;
  }


}
我的错误输出

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。我在想什么呢?

您不应该创建自己的
可克隆接口。您应该使用。

不要创建自己的可克隆公共界面。现在您得到的是您定义的,而不是系统。因此,您不是在实现“真正的”可克隆性,而是在实现自己的可克隆性,而Object.clone函数则不会将其视为使对象可克隆

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


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

为什么要创建自己的可克隆接口而不是使用Java核心库中的接口?实现Java.lang.cloneable而不是您创建的testbankaccount.cloneable。+1。另一种方法是声明
实现java.lang.Cloneable
(使用完全限定的接口名)。@Ted True。如果他真的有理由想创建自己的接口,而名称匹配只是巧合,那么这将使它起作用。但在这种情况下,他似乎没有理由创建这样的接口。就这样。当我从书中读到Cloneable是如何定义的时,我把它误认为是如何实现的。在这种情况下,我正在阅读,但没有完全吸收。谢谢大家。对不起,浪费了你的时间。
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)