雖然添加對象與參考名稱我收到錯誤爲什麼在集合接口中添加對象時,無法使用引用名稱創建對象?
class vechile
{
void service()
{
System.out.println("Generic vehicle servicing");
}
}
public class mechanic
{
public static void main(String args[])
{
List vehicles = new ArrayList();
vehicles.add(vechile q1=new vechile());// this line is showing error
vehicles.add(new vechile());
}
}
由於Java語法不允許它,因爲Java設計者認爲它沒有用。 –
請注意,在將來的問題中,如果您使示例代碼遵循正常的命名約定等(所以'Vehicle'而不是'vechile','Mechanic'而不是'mechanic'),這真的很有幫助。 。 –