所以。我正在尝试在我的超类中创建一个对象数组。让我们使用一个常见的例子:汽车。
我初始化了数组:Car[] cars = { new Car("Ford", "Sedan", "Focus", 17950) };
然而,这些数据正在使我的程序崩溃。我已经研究了几个小时了,但我找不到解决方案。要么是我的措辞不正确,要么就是我看得不够认真。
下面是代码的粘贴示例:https://pastebin.com/uE2wtL4R
public class Car {
// Data
protected String cMan, vType, name; // in order, car manufacturer, sedan, suv, name
protected double price;
// constructor
Car(String cMan, String vType, String name, double price) {
this.cMan = cMan;
this.vType = vType;
this.name = name;
this.price = price;
}
Car[] cars = { new Car("Ford", "Sedan", "Focus", 17950) };
}发布于 2019-02-23 04:42:21
这不是放置这行代码的有效位置:
Car[] cars = {new Car("Ford", "Sedan", "Focus", 17950)};它需要在方法体中(比如在main方法中)
https://stackoverflow.com/questions/54834824
复制相似问题