【发布时间】:2017-04-12 19:50:21
【问题描述】:
我已经定义了一个对象,然后从一个文件扫描器创建了许多对象。我通过使用 while 循环不断将这些对象添加到 ArrayList 中来存储它们以供以后使用来完成此操作。
我现在正在尝试将所述对象打印为字符串,我发现的问题是它具有各种属性(int、string、double、boolean、boolean、boolean)。
下面是对象:
class Room {
int roomNumber;
String roomType;
double roomPrice;
boolean roomBalcony;
boolean roomLounge;
boolean roomReserved;
public Room(int roomNumber, String roomType, double roomPrice, boolean roomBalcony, boolean roomLounge, boolean roomReserved) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.roomPrice = roomPrice;
this.roomBalcony = roomBalcony;
this.roomLounge = roomLounge;
this.roomReserved = roomReserved;
}
这是扫描仪。
while(file.hasNextLine()){
int roomNumber = file.nextInt();
String roomType = file.next();
double roomPrice = file.nextDouble();
boolean roomBalcony = file.nextBoolean();
boolean roomLounge = file.nextBoolean();
boolean roomReserved = false;
rooms.add(new Room(roomNumber, roomType, roomPrice, roomBalcony, roomLounge, roomReserved));
file.nextLine();}
file.close();
【问题讨论】:
-
具有整数、双精度和布尔值的对象到底有什么问题?
标签: java class oop object arraylist