car [ ] carForRent={new passengerCar(1,"奥迪A4",500,4),new passengerCar(2,"马自达6",400,4),
new pickUp(3,"皮卡雪6",450,4,2),new passengerCar(4," 金龙 ",800,20),
new trunk(5,"松花江",400,4),new trunk(6,"依维柯",1000,20)};
用了匿名类来给car数组初始化实例。
等效于:
Car car1=new passengerCar(1,"奥迪A4",500,4); Car car2=new passengerCar(2,"马自达6",400,4); Car car3=new passengerCar(3,"皮卡雪6",450,4,2); ... car[] carForRent = {car1,car2,car3,...}
数组的初始化本质上是给其填充实例化的子元素。
题目中的写法就是直接在声明数组语句的同时调用new 方法实例化数组中的元素,并填入数组中,而我的回答的方法则多走了一步,声明了car类型的变量,然后才在数组声明中把这些实例化后的变量塞给数组。
多了几个变量名,但效果是一样的。