/*
* 对象数组示例
* @author gyw
* */
public class MyArray5 {
public static void main(String[] args) {
Car[] myCar = new Car[3];//声明对象数组
//分别调用Car类构造器对各个元素初始化
myCar[0] = new Car("Audi A8 W12", 5267, 1949, 1471);
myCar[1] = new Car("Benz S600", 5230, 1871, 1485);
myCar[2] = new Car("BMW 760LI", 5212, 1902, 1484);
for(int i = 0; i < myCar.length; i++) {
System.out.println(myCar[i]);
}
}
}
class Car {
String type;
int length;
int width;
int heigth;
Car(String type, int length, int width, int heigth) {//构造方法
this.type = type;
this.length = length;
this.width = width;
this.heigth = heigth;
}
public String toString() {
return this.type + "\t" + this.length +
"\t" + this.width + "\t" + this.heigth;
}
}