2
我有實現Runnable
如下鑄造對象實現Runnable
Car implements Runnable{
int id;
public Car(int id) {
id = this.id;
}
public boolean getIsAvailable() {
return isAvailable;
}
}
class CabService{
static List<Thread> cars = new ArrayList<Thread>();
public static void main(String[] args) {
for(int i=0; i< 3; i++)
{
cars.add(new Thread(new Car(i)));
}
}
}
在CarService類我有車列表如下,並試圖遍歷列表,並把對象以汽車爲以下
Car對象List<Car> availableCars = new ArrayList<Car>();
List<Thread> cars = CabService.cars;
for(Thread carThread : cars) {
Car car = (Car)carThread;
if(car.getIsAvailable())
availableCars.add(car);
}
return availableCars;
當我嘗試投了carThread
到car
我得到的編譯錯誤Cannot cast from Thread to Car
有人能幫助我如何解決這一問題?
線程是一個類,而不是一個接口。所以你擴展線程不實現 – Peshal 2014-11-05 04:28:03
而'getIsAvailable()'不是'run()'。 – 2014-11-05 04:32:09
'Runnable'與'Thread'不一樣。 – immibis 2014-11-05 04:32:52