从抽象类和子类java创建对象数组
希望有人能够帮助我,或者为我指明正确的方向。 我有一个包含许多条目的文本文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在你的子类构造函数上。您必须显式调用所需的超类构造函数,否则编译器将添加
super()
作为子类构造函数中的第一个语句。下面是一个例子。和输出
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.and output
不,你不应该使用数组。请改用
List
,并对其简单的 API 感到满意。您可以使用它的add
和size
方法,并且不必自己跟踪大小。您的客户数量也可以超过 20 个,代码仍然有效。这是一些示例代码:
No, you should not use arrays. Use a
List<Customer>
instead and be happy about its simple API. You can use theadd
andsize
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:
这是我所犯的众多错误之一。当扩展我的客户类别时,我没有添加“super(cID, cName, cPhone)。这导致返回 null。
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.