从抽象类和子类java创建对象数组

发布于 2024-12-14 08:42:26 字数 1708 浏览 0 评论 0原文

希望有人能够帮助我,或者为我指明正确的方向。 我有一个包含许多条目的文本文件

CUST001,约翰·琼斯,555 0505,1981 年 9 月 19 日
CUST002,彼得·帕克,555 1234,0.2
CUST003,迈克尔·迈克尔斯,555 4321,1981 年 9 月 19 日
等等

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

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

我遇到的问题: 带有构造函数的常规类 - 这可以完美地工作。我在创建对象后将它们打印出来。

但我的子类,它只返回值 null、null、null 因此,我认为我的超类和子类存在问题。

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

产品[] prod = 新产品[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] = 新产品( tmpProd[0], 
                           tmpProd[1], 
                           tmpProd[2],
                           Float.parseFloat(tmpProd[3]),
                           tmpProd[4].charAt(0));
}

“尝试”从超类(Customer)和子类(STCustomer)创建对象:

客户[] 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(年、月-1、日期);
    stdCust[i] = new STCustomer( tmpCust[0],
                                 tmpCust[1], 
                                 Long.parseLong(tmpCust[2]), 
                                 d);//转换日期的块有效 - 此处省略
}  

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

客户[] stdCust= new STCustomer[20];

Hoping someone might be able to assist me, or point me in the right direction.
I have a text file with a number of entries

CUST001, John Jones, 555 0505, 19/09/1981
CUST002, PeterParker, 555 1234, 0.2
CUST003, Michael Michaels, 555 4321, 19/09/1981
etc

I have an abstract superclass with constructor and accessors for the shared properties and a subclass.
i then have another class, also with constructor and accessors.

I read in each line, and split it at the "," and read this into a temp array. I then create my empty array to read into it the properties from my superclass and with the contructor, i create the various objects.

problem i have run into:
regular class with constructor - this work prefectly. I print out my objects after they have been created and there they are.

My subclass though, it only returns values null, null, null
I assume therefore that there is an issue with my superclass and subclass.

creating objects using the concrete class constructor:

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));
}

"trying" to create objects from the superclass (Customer) and subClass (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
}  

Is this the correct way to create the objects?

Customer[] stdCust= new STCustomer[20];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-12-21 08:42:27

问题出在你的子类构造函数上。您必须显式调用所需的超类构造函数,否则编译器将添加 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)

The Problem is with your subclass constructor. You have to explicitly call the desired super class constructor, else the compiler will add super() as the first statement in you subclass constructor. Below is an example.

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());
    }
}

and output

Customer()
Customer()
Customer(String a, String b, long c,Date d)
杯别 2024-12-21 08:42:27

不,你不应该使用数组。请改用 List,并对其简单的 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());
}

No, you should not use arrays. Use a List<Customer> instead and be happy about its simple API. You can use the add and size methods of it, and you don't have to keep track of the size yourself. You can also have more than 20 customers, and the code will still work.

Here is some example code:

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

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

for (Customer customer : customers) {
  System.out.println(customer.getId());
}
享受孤独 2024-12-21 08:42:27

这是我所犯的众多错误之一。当扩展我的客户类别时,我没有添加“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;}

It was one of many mistakes I am making. When extending my customer class, i did not add "super(cID, cName, cPhone). This resulted in null being returned.

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;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文