本文介绍了类“ Room”是抽象的;无法实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个抽象类 Room
的类,该类具有 Family
和 Standard
,我在 Hostel
类中创建了 room = new ArrayList< Room>();
。我有一种向ArrayList添加房间的方法;
I have a class an abstract class Room
which has subclasses Family
and Standard
, I have created room = new ArrayList<Room>();
within a Hostel
class. I have a method to add a room to the ArrayList;
public String addRoom(String roomNumber, boolean ensuite)
{
if (roomNumber.equals(""))
return "Error - Empty name field\n";
else
room.add( new Room(roomNumber,ensuite) );
return "RoomNumber: " + roomNumber + " Ensuite: " + ensuite
+ " Has been added to Hostel " + hostelName;
}
但是我收到编译时间错误;
However I get the compile time error;
我知道不能实例化抽象类,但是添加房间的最佳方法是什么?
I understand that abstract classes cannot be instantiated, but what is the best way to add rooms?
推荐答案
出现此错误是因为您试图创建抽象类的实例,这是不可能的。您必须
You have this error because you are trying to create an instance of abstract class, which is impossible. You have to
room.add(new Family(roomNumber, ensuoute));
或
room.add(new Standard(roomNumber, ensuoute));
这篇关于类“ Room”是抽象的;无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!