【问题标题】:Reading input file into abstract superclass java将输入文件读入抽象超类java
【发布时间】:2017-02-01 17:22:50
【问题描述】:

这是我的项目,我必须实现继承、多态和 io。
希望有人可以帮助我。我有一个包含行李数据的文本文件。

本地;背包;NYC;黑色;45.00;5
本地;背包;瑞士 Polo;灰色;59.00;1
本地款;背包;Bill Keith;蓝色 50.00;2
进口;手提包;Padini;黄色;120.00;3
进口;手提包;PDI;红色; 170.00;2
进口;手提包;P&CO;Green;200.00;1

我有一个抽象超类,有构造函数、访问器和toString方法,根据数据分别由itemType、bagType、brand、color、price和quantity组成。

我还有 2 个子类,Import 和 Local,它们是从超类包中扩展而来的,它们从超类继承了相同的属性。

我无法将数据读入抽象超类的数组,因为它会说抽象类不能被实例化。是的,我知道这一点。那么我应该怎么做才能将数据读入数组呢?

这是我的抽象超类Bag:

public abstract class Bag
{
    private String bagType;
    private String colour;
    private String brand;
    private int quantity;
    private double price;
    
    //default constructor
    public Bag()
    {
        bagType = " ";
        colour = " ";
        brand = " ";
        quantity = 0;
        price = 0.00;
    }

    //normal constructor
    public Bag(String bagType, String brand, String colour, double price, int quantity)
    {
        this.bagType = bagType;
        this.brand = brand;
        this.colour = colour;
        this.price = price;
        this.quantity = quantity;
    }

    //accessor method
    public String getColour()
    {
        return colour;
    }
    public String getBrand()
    {
        return brand;
    }
    public int getQuantity()
    {
        return quantity;
    }
    public double getPrice()
    {
        return price;
    }
    
    //mutator method
    public void setBagType(String bt)
    {
        bagType = bt;
    }
    public void setColour(String c)
    {
        colour = c;
    }
    public void setBrand(String b)
    {
        brand = b;
    }
    public void setQuantity(int q)
    {
        quantity = q;
    }
    public void setPrice(double p)
    {
        price = p;
    }

    //abstract method
    public abstract double calcPrice();

    //toString method
    public String toString()
    {
        return
        "\nBag Type      : " + bagType +
        "\nItem Colour   : " + colour +
        "\nItem Brand    : " + brand +
        "\nItem Quantity : " + quantity +
        "\nPrice         : " + price;
    }
}

这是我的测试课:

import java.util.*;
import java.io.*;

public class TestBag
{
    public static void main(String [] args)
    {
        try
        {
            BufferedReader in = new BufferedReader(new FileReader("Bag.txt"));

            PrintWriter Import = new PrintWriter(new BufferedWriter(new FileWriter("Import.txt")));
            PrintWriter Local = new PrintWriter(new BufferedWriter(new FileWriter("Local.txt")));
            
            String data = null;
            int size = 0;
            while((data = in.readLine()) != null)/**read data to count the no of records*/
            {
                StringTokenizer input = new StringTokenizer(data,";");
                size++;

            }
            in.close();
            
            /**reopen the input file*/
            in = new BufferedReader(new FileReader("Bag.txt"));
            Bag b [] = new Bag[size];
            int index = 0;
            while((data = in.readLine()) != null) //read the contents of file and process the data
            {
                StringTokenizer inData = new StringTokenizer(data,";");
                
                String type = inData.nextToken();
                String bagType = inData.nextToken();
                String brand = inData.nextToken();
                String colour = inData.nextToken();
                double price = Double.parseDouble(inData.nextToken());
                int quantity = Integer.parseInt(inData.nextToken());
                
                if(type.equalsIgnoreCase("Import")) //Import
                {   
                    b[index] = new Import(bagType, brand, colour, price, quantity);
                    System.out.println(b[index].toString());
                    
                }
                else if(type.equalsIgnoreCase("Local"))
                {
                    b[index] = new Local(bagType, brand, colour, price, quantity);
                    System.out.println(b[index].toString());
                }
                
                index++;
            }
            
            //
            
            in.close();
            Import.close();
            Local.close();
            
        }
        //end of try
        catch(FileNotFoundException fnfe)
        { System.out.print(fnfe.getMessage());}
        catch(IOException io)
        { System.out.print(io.getMessage());}
        catch(Exception e)
        { System.out.print(e.getMessage());}
    }

}

对不起。我还是 java 新手。

【问题讨论】:

  • 我在您发布的代码中看不到抽象类。
  • 抽象类在超类 Bag 类中,这是我测试代码的类 testBag。
  • 对不起,我的水晶球坏了,所以我看不到你的Bag课程,除非你出示。
  • 我已经更新了我的Bag 课程

标签: java inheritance polymorphism abstract-class superclass


【解决方案1】:

首先,在这里阅读抽象类:Abstract Methods and Classes,我也没有在你的代码中看到任何承包商......你必须自己创建它们吗?

【讨论】:

  • 抽象类在超类 Bag 类中,这是我测试代码的类 testBag。所有的构造函数也在超类 Bag 和子类 Import、Local 上。
【解决方案2】:

如果你的Bag是抽象的,那么就不可能调用new Bag()

您需要检查 bagType 并根据它创建一个类。

if(type.equals("import")){
   b[index] = new ImportBag (type, bagType, brand, colour, price, quantity);
} else {
   b[index] = new LocalBag (type, bagType, brand, colour, price, quantity);
}

Arraylist 可能比 Array 更有用。

【讨论】:

  • 在您的拼写错误和您对@OP 拥有/想要什么的假设之间,这是一个不合标准的答案
  • 我很抱歉拼写错误。英语不是我的母语,我用智能手机写了这篇文章。你能告诉我如何改进我的答案吗?
  • 我已经为子类创建了数组,但是当我尝试编译时,出现For input string: " 5"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 2011-07-17
相关资源
最近更新 更多