java:类中的构造函数不能应用于给定的类型

时间:2013-11-04 11:11:38

标签: java constructor compiler-errors

当我尝试用maven编译我的代码时,我收到以下错误:

constructor TripGuesser in class TripGuesser cannot be applied to given types

但是在Eclipse中(实际上是FuseIDE)它不会引发错误!

我的代码是这样的:

public class TripGuesser {
   private ArrayList<VehicleAbstract> vehicles;
   private DataAccessHandler dataAcces;
   private String stop_id;

   @SuppressWarnings("unchecked")
   public TripGuesser(ArrayList<? extends VehicleAbstract> vehicles, DataAccessHandler dataAccess, String stop_id){
          this.vehicles = (ArrayList<VehicleAbstract>) vehicles;
          this.dataAcces = dataAccess;
          this.stop_id = stop_id;
    }
}


public class HelsinkiStopsHandler {
    ...
    ArrayList<Vehicle> vehicles = vehiclesPerRoute.get(routeAndDirectionId);
    TripGuesser guesser = new TripGuesser(vehicles, dataAccess, last_stopId);
    ...
}

public class Vehicle extends VehicleAbstract{
       ...
}

public abstract class VehicleAbstract{
       ...
}

我已经尝试过不同的方式调用构造函数,所以我的类HelsinkiStopsHandler看起来像:

public class HelsinkiStopsHandler  {
    ArrayList<? extends VehicleAbstract> vehicles = vehiclesPerRoute.get(routeAndDirectionId);
    TripGuesser guesser = new TripGuesser(vehicles, dataAccess, last_stopId);
}

但我仍然得到同样的错误。

我知道这里有很多熟悉的问题,但这些答案都不适合我...

Java版本:1.7.0

Fuse IDE版本:6.0.0

使用maven版本编译:3.1.0

1 个答案:

答案 0 :(得分:0)

我找到了解决方案...... 当我的代码看起来像:

 public class TripGuesser {
     private ArrayList<VehicleAbstract> vehicles;
     private DataAccessHandler dataAcces;
     private String stop_id;

     @SuppressWarnings("unchecked")
     public TripGuesser(ArrayList<VehicleAbstract> vehicles, DataAccessHandler dataAccess, String stop_id){
      this.vehicles = vehicles;
      this.dataAcces = dataAccess;
      this.stop_id = stop_id;
     }
 }


 public class HelsinkiStopsHandler {
      ...
      ArrayList<? extends VehicleAbstract> vehicles = vehiclesPerRoute.get(routeAndDirectionId);
      ArrayList<VehicleAbstract> vehiclesAbs = (ArrayList<VehicleAbstract>) vehicles;
      TripGuesser guesser = new TripGuesser(vehiclesAbs, dataAccess, last_stopId);
      ...
 }