2011-11-10 146 views
0

希望有人能够帮助我,或指出我的方向正确。 我有许多条目从抽象和子类创建对象数组java

CUST001,约翰·琼斯,555 0505的一个文本文件,19/09/1981
CUST002,PeterParker,555 1234,0.2
CUST003,迈克尔·迈克尔斯,555 4321, 19/09/1981

我有构造器和访问器共享的属性和子类的抽象超类。 然后我有另一个类,也有构造函数和访问器。

我在每行中读取,并将其拆分到“,”并将其读取到一个临时数组中。然后,我创建我的空数组,以从我的超类和构造函数中读入属性,创建各种对象。

问题我碰到: 常规类与构造函数 - 这项工作熟练。我将他们创建完成后的对象打印出来,在那里。

虽然我的子类,它只返回值null,null,null 因此,我认为我的超类和子类有一个问题。

使用具体类的构造函数创建对象:

Product[] prod = new Product[20]; 
BufferedReader inFile = new BufferedReader (new FileReader("product.txt")); 
String inputLine = inFile.readLine(); 
for (int i = 0; i < 6 ; i++) 
{ 
    String[] tmpProd = inFile.readLine().split(","); 
    prod[i] = new Product(tmpProd[0], 
          tmpProd[1], 
          tmpProd[2], 
          Float.parseFloat(tmpProd[3]), 
          tmpProd[4].charAt(0)); 
} 

“试图” 从父(客户)和子类(STCustomer)创建对象:

Customer[] stdCust= new STCustomer[20]; 
BufferedReader inFileCust = new BufferedReader (new FileReader ("customer.txt")); 
String inputCust = inFileCust.readLine(); 
for (int i = 0; i < 6; i++) 
{ 
    String[] tmpCust = inFileCust.readLine().split(","); 
    GregorianCalendar d = new GregorianCalendar(year, month -1, date); 
    stdCust[i] = new STCustomer( tmpCust[0], 
           tmpCust[1], 
           Long.parseLong(tmpCust[2]), 
           d);//the block to convert date works - omitted here 
} 

这是创建对象的正确方法吗?

Customer[] stdCust= new STCustomer[20]; 
+0

你可以重命名所有在你的问题中的“常规类”的措辞,要么说“子类”或“超类”我不知道常规类是什么。在某些部分,它听起来像是指超级类别,而在另一些部分,它听起来像是指的是子类别。 –

+0

嗨乔治,道歉的混乱。我有两个我创建的课程。一个是具体类,另一个是具有子类的抽象类。我正尝试使用子类从文本文件创建对象。对具体类(产品)做同样的事情,可以让我用txt文件中的值成功创建对象。这就是我所坚持的。看来我失去了一些明显的东西,因为我没有看到其他人遇到这些困难。 – user1031551

回答

2

该问题是否与你的子类的构造函数。您必须显式调用所需的超类构造函数,否则编译器会将super()作为第一个语句添加到您的子类构造函数中。下面是一个例子。

import java.util.Date; 

public class Test { 
     public static void main(String... abc){ 
      Customer[] a = new STCustomer[20]; 
      a[0] = new STCustomer(); 
      a[1] = new STCustomer("Hello","World",12L,new Date()); 
      a[1] = new STCustomer("Hello","World",12L); 
     } 
} 

class Customer{ 
    public Customer(){ 
     System.out.println("Customer()"); 
    } 

    public Customer(String a, String b, long c,Date d){ 
     System.out.println("Customer(String a, String b, long c,Date d)"); 
      // Set values to fields 
    } 
} 

class STCustomer extends Customer{ 
    public STCustomer(){} 

    public STCustomer(String a, String b, long c,Date d){ 

    } 

    public STCustomer(String a, String b, long c){ 
     super(a,b,c,new Date()); 
    } 
} 

和输出

Customer() 
Customer() 
Customer(String a, String b, long c,Date d) 
0

不,你不应该使用数组。改为使用List<Customer>,并对其简单的API感到满意。您可以使用它的addsize方法,而且您不必自己跟踪大小。您也可以拥有20多个客户,并且代码仍然可以工作。

下面是一些示例代码:

List<Customer> customers = Lists.newArrayList(); 

... 
while ((line = bufferedReader.readLine()) != null) { 
    ... 
    customers.add(new Customer(...)); 
} 

for (Customer customer : customers) { 
    System.out.println(customer.getId()); 
} 
0

这是许多错误,我想提出一个。在扩展我的客户类时,我没有添加“super(cID,cName,cPhone)”,导致返回null。

class STCustomer extends Customer{ 
//instance variables 
private GregorianCalendar stCustJoinDate; 

//constructor 
public STCustomer (String cID, String cName, String cPhone, 
     GregorianCalendar stCJoinDate) 
{ 
    super(cID, cName, cPhone); 
    stCustJoinDate = stCJoinDate; 
} 

//accessor 
public GregorianCalendar getSTCJoinDate() {return stCustJoinDate;}