[英]inheritance and polymorphism java
我有基類,客戶類和子類Acc1到3。
當我讀取文件以初始化我的客戶對象數組時,它們全部都顯示為空。 當我找出客戶擁有哪種帳戶類型后,便將變量分配給相應的對象(temp1-3,first1-3)。 不太確定為什么它們保持空白。
我正在為基類和派生類之一粘貼我的類定義。 當我將first和temp分配給first(1/2/3)和temp(1/2/3)(子類,Account1,Account2和Account3)時,在readFile()方法中發生錯誤。
不確定類定義是否錯誤,因為我只是在所有定義中都調用了超級構造函數。 嘗試調試它,但沒有成功。 幫助將不勝感激。
package lab4;
import java.io.*;
import java.util.*;
public class lab4{
static int count =0; // number of records read or written
public static void main(String args[])
throws IOException
{
Customer[] records = new Customer[30];
for (int j=0; j<30; j++){
records[j] = new Customer();
}
menu(records);
}
public static int readFile(String filename, Customer[] review)
throws IOException
{
Scanner scan = new Scanner (new File (filename));
/*Reading the first record separatly*/
Customer first = new Customer();
Account1 first1= new Account1();
Account2 first2= new Account2();
Account3 first3 = new Account3();
String[] a = scan.nextLine().split("=");
first.set_account_id(Integer.parseInt(a[1].trim()));
a = scan.nextLine().split("=");
first.set_name(a[1].toUpperCase().trim());
a = scan.nextLine().split("=");
first.set_address(a[1].trim());
a = scan.nextLine().split("=");
first.set_phone_number(a[1].trim());
a = scan.nextLine().split("=");
first.set_date_of_birth(a[1].trim());
a = scan.nextLine().split("=");
first.set_balance(Double.parseDouble(a[1].trim()));
a= scan.nextLine().split("=");
first.set_accType(a[1].trim());
if (first.get_accType().equals("Saving")){
first = first1;
}
else if(first.get_accType().equals("Checking")){
first = first2;
}
else if(first.get_accType().equals("Fixed")){
first = first3;
a = scan.nextLine().split("=");
first3.set_intRate(Double.parseDouble(a[1].trim()));
}
System.out.println(first.get_name());
scan.nextLine();// resets the buffer reader
review[0]= first;
count = count+1;
while (scan.hasNext()&& count>0){
Customer temp = new Customer();
Account1 temp1 = new Account1();
Account2 temp2 = new Account2();
Account3 temp3 = new Account3();
String[] st = scan.nextLine().split("=");
for(int i=0;i<count;i++){
if(Integer.parseInt(st[1].trim())== review[i].get_accountid()){ // checking for duplicate records
System.out.println("This account id is already in use so the record won't be read");
for (int k=0; k<7; k++)
scan.nextLine();
}
else
break;
}
temp.set_account_id(Integer.parseInt(st[1].trim()));
st = scan.nextLine().split("=");
temp.set_name(st[1].toUpperCase().trim());
st = scan.nextLine().split("=");
temp.set_address(st[1].trim());
st = scan.nextLine().split("=");
temp.set_phone_number(st[1].trim());
st = scan.nextLine().split("=");
temp.set_date_of_birth(st[1].trim());
st = scan.nextLine().split("=");
temp.set_balance(Double.parseDouble(st[1].trim()));
st= scan.nextLine().split("=");
temp.set_accType(st[1].trim());
if (temp.get_accType().equals("Saving")){
temp = temp1;
}
else if(temp.get_accType().equals("Checking")){
temp = temp2;
}
else if(temp.get_accType().equals("Fixed")){
temp = temp3;
st = scan.nextLine().split("=");
temp3.set_intRate(Double.parseDouble(a[1].trim()));
}
if (scan.hasNextLine()){
scan.nextLine();
}
int j;
for(j=0;j<count;j++){
if (temp.get_name().compareTo(review[j].get_name())<0){ // Putting records in ascending order
break;
}
}
count=count+1;
for (int k=count;k>j;k--){
review[k]=review[k-1];
}
review[j]= temp;
if (count>=30){
System.out.println("The number of records read has exceeded the limit and it will stop reading now");
break;
}
}
return count;
}
}
package lab4;
import java.io.*;
import java.util.*;
/**
*
* @author dawnoflife
*/
public class Account1 extends Customer {
public Account1(){ // Savings Account
super();
}
public void update(double rate){ // Savings account interest calc
double updateBal = (this.get_balance()*( Math.pow((1+rate),31))); // Interest calculated for month of march
this.set_balance(updateBal);
}
}
因此,您正在將信息讀入first
變量的Customer
對象中,然后使用first = first1
。 (或first2
, first3
)。
稍后,您對temp
和temp1
(或temp2
, temp3
)執行相同的操作。
我認為您誤解了=
運算符的含義。 它不會將現有first
對象的類更改為first1
的類,而是將變量中的指針從現有對象切換到另一個對象。
之前:
.------------.
first -----> | Customer |
'------------'
.------------.
first1 ----> | Account1 |
| |
'------------'
后:
.------------.
first | Customer |
\ '------------'
\
\ .------------.
'---> | Account1 |
first1 ----> | |
'------------'
現在,“ Customer
對象中的所有信息都消失了。 (這同樣適用於其他帳戶類型,以及temp
更高版本。)
看來您必須執行以下一項操作:
在決定使用哪種帳戶類型時,您必須將數據復制到您的帳戶中。
您可以為此使用副本構造函數。
更改文件格式,使其首先包含帳戶類型,並在開始時創建正確類型的對象。
順便說一下,考慮一下設計-為什么Account1
是Customer
的子類? 一位顧客不是一個帳戶,他有一個 。
因此,最好在這里使用委派,並考慮信息的哪一部分是帳戶的一部分,哪一部分是客戶的一部分。 這樣,一個客戶甚至可以擁有多個帳戶(甚至是不同類型的帳戶)。
您的代碼很難閱讀。 您遇到的問題之一是,當您為該對象重新分配其他值時,丟失了使用該對象的方法設置的信息。
請查看我對繼承和多態Java的回答,我相信它將幫助您解決這個問題。
您要嘗試做的是根據您從掃描文件中解析得到的一些值來建立類型為Account1,Account2,Account3的Customer對象。 首先要做的是構造正確類型的對象。 然后在特定於該類型的方法中設置值。 然后在超類Customer中的方法中設置值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.