java - java的继承与多态

标签 java oop

我有基类、客户和子类 Acc1 到 3。

当我读取文件来初始化客户对象数组时,它们都显示为空。当我发现客户拥有什么帐户类型时,我将变量分配给相应的对象(temp1-3,first1-3)。不太清楚为什么它们仍然是空的。

我正在粘贴基类和派生类之一的类定义。当我分配第一个和临时(客户类对象_到第一个(1/2/3)和临时(1/2/3)(子类,Account1,Account2和Account3)时,readFile()方法中发生错误。

不确定类定义是否错误,因为我只是在所有类定义中调用 super 构造函数。尝试调试它但没有成功。我们将不胜感激。

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 将其丢弃。 (或first2first3)。

稍后,您可以对 temptemp1(或 temp2temp3)执行相同的操作。

我认为您误解了 = 运算符的含义。它不会将现有 first 对象的类更改为 first1 类,但会将变量中的指针从现有对象切换到另一个对象。

之前:

             .------------.
first -----> | Customer   |
             '------------'

             .------------.
first1 ----> | Account1   |
             |            |
             '------------'

之后:

             .------------.
first        | Customer   |
    \        '------------'
     \
      \      .------------.
       '---> | Account1   |
first1 ----> |            |
             '------------'

Customer 对象中的所有信息现已消失。 (这同样适用于其他帐户类型,以及稍后的 temp。)

看来您必须执行以下操作之一:

  • 在决定使用哪种帐户类型时,您必须将数据复制到您的帐户。

    您可以为此使用复制构造函数。

  • 更改文件格式以首先包含帐户类型,然后在开始时创建正确类型的对象。

<小时/>

顺便想一下设计 - 为什么 Account1Customer 的子类?客户不是帐户,他有一个

所以这里最好使用委托(delegate),并思考哪部分信息是帐户的一部分,哪部分是客户的部分。然后,一个客户甚至可以拥有多个帐户(甚至是不同类型的帐户)。

关于java - java的继承与多态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5449262/

相关文章:

java - 面向对象编程是如何工作的?

c# - IoC、SRP 和组合——我是否创建了太多接口(interface)?

Java子字符串使用lastindexOf返回特定长度

java - 传递方法引用 NoClassDefFoundError

java - Mysql 表对象列表到字符串数组通过 System.arraycopy 得到 ArrayStoreException?

Python:如何区分继承的方法

Java重写疑问

java - 使用 Spring Data Rest 和 MongoDB 更新特定字段

java - 如何正确处理人类可读的结果?

c++ - c++中的组合错误