在我发布这篇文章之前,我先阅读了一些以前的帖子,但我的逻辑并没有发现任何问题。 (我已经花了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。我没想到什么?
答案 0 :(得分:5)
您不应该创建自己的Cloneable
界面。您应该使用one that's built-in。
答案 1 :(得分:1)
不要创建自己的公共接口Cloneable。现在你得到的是你定义的而不是系统的。因此,不是实现“真正的”Cloneable,而是实现自己的,而Object.clone函数则无法将其识别为使对象可克隆。
您还需要为您希望能够克隆的每个类编写一个公共克隆函数,以覆盖从私有到公共的保护。
由于我不理解的原因,Java使得对象可以克隆是一件非常痛苦的事。