【发布时间】: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