作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class TestingClass {
public static void main(String[] args) {
int numberRooms = 6;
double totalSpace;
Room[] rooms = new Room[numberRooms];
rooms[0] = new Room(20, 20);
rooms[1] = new Room(20, 20);
rooms[2] = new Room(22, 20);
rooms[3] = new Room(20, 20);
rooms[4] = new Room(25, 20);
rooms[5] = new Room(15, 13);
Garage garage = new Garage(20, "Cement", 1, 20, 1, 4);
for (int i = 0; i < numberRooms; i++) {
totalSpace = totalSpace + calculateRoomSpace(rooms[i]);
}
System.out.println("Total room space is " + totalSpace);
foo(garage);
}
public static double calculateRoomSpace(Room roomSpace) {
double newRoomSpace = roomSpace.calcFloorSpace();
return newRoomSpace;
}
public static void foo(Building myBuilding) {
System.out.println("Total floor space is " + myBuilding.calcFloorSpace());
}
}
我输入这段代码并得到错误
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type Garage
at TestingClass.main(TestingClass.java:17)"
这里到底有什么问题?
编辑** 这是车库类**
abstract public class Garage extends Building {
private int cars;
private String floorType;
private int garageLength;
private int garageWidth;
public Garage(int floors, int windows, int cars, String floorType,
int garageLength, int garageWidth) {
super(floors, windows);
this.cars = cars;
this.floorType = floorType;
this.garageLength = garageLength;
this.garageWidth = garageWidth;
}
@Override
public double calcFloorSpace() {
int floorSize;
floorSize = garageLength * garageWidth;
return floorSize;
}
public int getCars() {
return cars;
}
public void setCars(int cars) {
this.cars = cars;
}
public String getFloorType() {
return floorType;
}
public void setFloorType(String floorType) {
this.floorType = floorType;
}
public int getGarageLength() {
return garageLength;
}
public void setGarageLength(int garageLength) {
this.garageLength = garageLength;
}
public int getGarageWidth() {
return garageWidth;
}
public void setGarageWidth(int garageWidth) {
this.garageWidth = garageWidth;
}
@Override
public String toString() {
return "Garage [cars=" + cars + ", floorType=" + floorType
+ ", garageLength=" + garageLength + ", garageWidth="
+ garageWidth + "]";
}
}
希望这对您有所帮助。不是这方面的专家,并且花了很长时间才弄明白。谢谢
最佳答案
你不能实例化一个抽象类。
你有两个选择:
abstract
,或者Garage
,它可以被实例化。关于java - "Cannot instantiate the type....",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17801609/
我是一名优秀的程序员,十分优秀!