java的继承和多态性
我有基类、客户和子类Acc1,直到3 当我读取一个文件来初始化客户对象数组时,它们都显示为空。当我找出客户的账户类型时,我将变量分配给相应的对象(temp1-3,first1-3)。不太清楚为什么它们仍然是空的 我正在粘贴基类和一个派生类的类定义。当我将first和temp(客户类对象)分配给first(1/2/3)和temp(1/2/3)(子类Account1、Account2和Account3)时,readFile()方法中会发生错误 我不确定类定义是否错误,因为我只是在所有类中调用了超级构造函数。我尝试过调试它,但没有成功。非常感谢您的帮助java的继承和多态性,java,oop,Java,Oop,我有基类、客户和子类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);
}
}
lab4包装;
导入java.io.*;
导入java.util.*;
公共类lab4{
静态int count=0;//读取或写入的记录数
公共静态void main(字符串参数[])
抛出IOException
{
客户[]记录=新客户[30];
对于(int j=0;j0){
客户临时工=新客户();
Account1 temp1=新Account1();
Account2 temp2=新Account2();
Account3 temp3=新Account3();
字符串[]st=scan.nextLine().split(“”);
对于(inti=0;i,您的代码很难阅读。
您遇到的问题之一是,当您将不同的值重新指定给某个对象时,您正在丢失使用该对象的方法设置的信息
请看我的答案,我相信这将帮助你解决这个问题
您试图做的是根据扫描文件时解析的某个值,构建Account1、Account2、Account3类型的Customer对象。
首先要做的是构造正确类型的对象。
然后在特定于该类型的方法中设置值。
然后在超类Customer中的方法中设置值。您的代码很难阅读。
您遇到的问题之一是,当您将不同的值重新指定给某个对象时,您正在丢失使用该对象的方法设置的信息
请看我的答案,我相信这将帮助你解决这个问题
您试图做的是根据扫描文件时解析的某个值,构建Account1、Account2、Account3类型的Customer对象。
首先要做的是构造正确类型的对象。
然后在特定于该类型的方法中设置值。
然后在超类Customer中的方法中设置值。因此,您将信息读入first
变量中的Customer
对象,然后将其与first=first1
一起丢弃(或first2
,first3
)
稍后您将对temp
和temp1
(或temp2
,temp3
)执行相同的操作
我认为您误解了=
运算符的含义。它不会将现有first
对象的类更改为first1
的类,但会将变量中的指针从现有对象切换到另一个对象
之前:
.------------.
first -----> | Customer |
'------------'
.------------.
first1 ----> | Account1 |
| |
'------------'
之后:
.------------.
first | Customer |
\ '------------'
\
\ .------------.
'---> | Account1 |
first1 ----> | |
'------------'
Customer
对象中的所有信息现在都消失了。(这同样适用于其他帐户类型,以及以后的temp
)
看起来您必须执行以下操作之一:
- 在决定使用哪种帐户类型时,您必须将数据复制到您的帐户
您可以为此使用复制构造函数
- 首先更改文件格式以包含帐户类型,并在开始时创建正确类型的对象
顺便说一句,想想设计-为什么Account1
是Customer
的子类?客户不是账户,他有账户
因此,最好在这里使用委托,并考虑哪部分信息是帐户的一部分,哪部分是客户的一部分因此,您正在将信息读入first
变量中的Customer
对象,然后将其与first=first1
一起丢弃(或first2
,first3
)
稍后您将对temp
和temp1
(或temp2
,temp3
)执行相同的操作
我认为您误解了=
运算符的含义。它不会将现有first
对象的类更改为first1
的类,但会将变量中的指针从现有对象切换到另一个对象
之前:
.------------.
first -----> | Customer |
'------------'
.------------.
first1 ----> | Account1 |
| |
'------------'
之后:
.------------.
first | Customer |
\ '------------'
\
\ .------------.
'---> | Account1 |
first1 ----> | |
'------------'
Customer
对象中的所有信息现在都消失了。(这同样适用于其他帐户类型,以及以后的temp
)
看起来您必须执行以下操作之一:
- 在决定使用哪种帐户类型时,您必须将数据复制到您的帐户
您可以为此使用复制构造函数
- 首先更改文件格式以包含帐户类型,并在开始时创建正确类型的对象
顺便说一句,想想设计-为什么Account1
是Customer
的子类?客户不是账户,他有账户
因此,最好在这里使用委托,并考虑哪部分信息是帐户的一部分,哪部分是客户的一部分.可能会对其中一些代码进行总结,因为我怀疑所有代码是否都与您的错误直接相关。总结的意思是省略与所描述的问题无关的部分。任何人都不太可能仅仅为了回答您的问题而阅读300多行代码。对不起