[英]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.