2016-11-24 15 views
-1

私はLecturingクラスの例を試しています。javaで拡張を使用して、さまざまなオブジェクトの配列を作成しようとしています。Javaはエラーに互換性のない型を拡張します

Carクラス::

public class Car extends RoadVehicle { 
    //single instance variable make 
    private String make; 

    /** 
    * Constructor takes the parameters make 
    * which describes the make of car 
    */ 
    public Car (String make, String colour) { 
     //sets up initial make 
     super (colour); 
     this.make = make; 
    } 

    /** get the make 
    */ 
    public String getMake() { 
     return make; 
    } 

    public String toString() { 
     return "The colour of the " + make +" is "+ colour; 
    } 

    public static void main(String[] args) { 
     Car car1 = new Car ("Ferrari", "Red"); 
     System.out.println(car1); 
     Car car2 = new Car ("Volvo", "Blue"); 
     car2.addFuel(20); 
     System.out.println(car2); 
     Car car3 =car2; 
     car2.setColour("Green"); 
     car3.useFuel(10); 
     System.out.println(car2); 
     System.out.println(car3); 
    } 
} 

Busクラス:

public class Bus extends RoadVehicle { 
    private int seats;// number of seats on bus 

    public Bus (int seats, String colour) { 
     super (colour); 
     this.seats = seats; 
    } 

    public int getSeats() { 
     return seats; 
    } 

    public String toString() { 
     return "The " + colour +" bus with "+ seats +" seats."; 
    } 

    public static void main(String[] args) { 
     Bus bus1 = new Bus (10, "Red"); 
     System.out.println(bus1); 
    } 
} 

RoadVehicleクラス:

/** Class to represent the colour and fuel content of the car 
*/ 
public abstract class RoadVehicle { 
    protected String colour; 
    private int fuel; 

    /** 
    * Constructor takes the parameters colour and fuel, 
    * which describes the car and amount of fuel 
    */ 
    public RoadVehicle(String colour) { 
     //sets up initial fuel and colour 
     this.colour = colour; 
     this.fuel = 0; 
    } 

    /** 
    * Get the colour 
    */ 
    public String getColour() { 
     return colour; 
    } 

    /** 
    * get the amount of fuel 
    */ 
    public int getFuel() { 
     return fuel; 
    } 

    /** 
    * Change the colour of the car to new colour, 
    * there is no check that the string is a sensible car colour 
    */ 
    public void setColour (String newColour){ 
     colour = newColour; 
    } 

    /** 
    * Chaging the amount of fuel in the car via adding integer 
    * We have no check for negative numbers 
    */ 
    public void addFuel (int amount) { 
     fuel = fuel + amount; 
    } 

    /** 
    * Change the amount of fuel via subtracting integer 
    * We have no check for negative numbers 
    */ 
    public void useFuel(int amount) { 
     fuel = fuel - amount; 
    } 

    /** 
    * Show the make and colour of the car, along with fuel remaining 
    */ 
    public abstract String toString(); 

} 

GoodsVehicleクラス

私はかなりそれはので、私のコードは説明カントその後、
public class GoodsVehicle extends RoadVehicle { 
    private int maxWeight; 
    private String typeOfVehicle; 

    public GoodsVehicle (int maxWeight, String typeOfVehicle, String colour) { 
     super (colour); 
     this.maxWeight=maxWeight; 
     this.typeOfVehicle=typeOfVehicle; 
    } 

    public int getMaxWeight() { 
     return maxWeight; 
    } 

    public String getVehicleType() { 
     return typeOfVehicle; 
    } 

    public String toString() { 
     return colour + " " + typeOfVehicle + " max Weight = " + maxWeight; 
    } 

    public static void main(String[] args) { 
     GoodsVehicle vechile1 = new GoodsVehicle (10, "van", "Red"); 
     System.out.println(vechile1); 
    } 

TrafficQueueクラス:簡単に言えば

private RoadVehicle [] Vehicles; 
private RoadVehicle [] Cars2; 
private static int numberOfCars; // has to be static for while loop 
private String make, colour; 

public void add(RoadVehicle car) { 
    if(numberOfCars==Vehicles.length) { 
    System.out.println("Queue is full! We cannot add any more cars to the Queue!"); 
    System.exit(1); 
    } else { 
    Vehicles[numberOfCars] = car; 
    numberOfCars ++; 
    System.out.println("Car added to queue. Number of cars is: " + numberOfCars); 

public static void resetNumberOfCars() { 
    numberOfCars = 0 ; 
} 


/** 
* To test the program we have made 
*/ 
public static void main (String [] args) { 

    TrafficQueue queue1 = new TrafficQueue(6); 

    Car car1 = new Car ("car", "red"); 
    Car car2 = new Car ("car2", "green"); 
    Bus bus1 = new Bus (2, "red"); 
    Bus bus2 = new Bus (3, "green"); 
    GoodsVehicle vehicle1 = new GoodsVehicle(5, "GoodsVehicle1", "red"); 
    GoodsVehicle vehicle2 = new GoodsVehicle (5,"GoodsVehicle2", "green"); 

    queue1.add(car1); 

は、私がCarBusRoadVehicleクラスを持っています。これらはすべて別のクラスVehiclesから拡張されています。私はそれぞれのクラスに2つのオブジェクトを作成していますが、それをに追加しようとしています。

incompatible types:m Car cannot be converted to RoadVehicle.

コードは、この時点まで正常に動作しますので、それをある私が行方不明だということは、私が追加することはできません:私はちょうど上記をしようとした場合

は、それが次のエラーを与えます相違VehiclesTrafficQueue

+2

それぞれのクラスの定義を表示できますか( 'Car Carriers extend BLAHBLAH'など)? 'TrafficQueue'が' RoadVehicle'sを期待するなら、 'Car'がそのクラス/インターフェースを拡張/実装することを確かめてください。 – devnull69

+2

これは実際の継承構造を見るのに役立ちます。 –

+0

与えられたコードは、なぜそれが起きたのかを明確にしています。ここで使用しているすべてのクラスを質問の中に追加するか、それとも関連する部分を追加するべきでしょう。 – SomeJavaGuy

答えて

-1

ちょうどタイプミスか、VehicleRoadVehicleが混在しましたか?

ヨールCarクラスで宣言されなければならないpublic class Car extends RoadVehicle {...}

あなたTrafficQueue確かVehicleを拡張し、add()が、それはRoadVehicleが延びていても動作しませんRoadVehicleを期待public void add(RoadVehicle v) {...}

Car場合は機能を持っている必要がありVehicleRoadVehicleを以来(予想される)はVehicle(提供される)より具体的です。

+0

申し訳ありませんguyhsは、正しい形式でコードを取得するのに苦労していました! –

+0

'Car'の宣言はまだありません。 「RoadVehicle」を拡張していますか? –

+0

宣言はありませんか?私の「車」クラスのトップに? これは 'RoadVehicle'を拡張し、' TrafficQueue'の中で上記のように新しい車を作ることができ、コードがコンパイルされます。 –

関連する問題