【发布时间】:2014-03-05 23:49:46
【问题描述】:
我有这门课
class Customer{
int ID;
Time arriveTime;
Time serviceTime;
Time completeTime;
int transaction;
}
我不需要构造函数来设置里面的值吗?我将使用这个类来保存不同的值,同时制作一个客户数组。这是否意味着我需要这个来设置值?
public Customer(int id, Time arrive, Time service, Time complete, int trans){
ID = id;
arriveTime = arrive;
serviceTime = service;
completeTime = complete;
transaction = trans;
}
我只需要 Customer 类来保存每个客户的信息。
【问题讨论】:
-
除非您指定 some 构造函数,Java 提供了一个隐式的无参数构造函数,它只是将所有内容设置为零。
-
请注意,以上内容完全合法地等效于 C/C++“结构”——一个不完全的对象,它将数据项组合在一起而不封装它们。并非所有情况都需要封装——有时这只是额外的工作。
标签: java class constructor