简体   繁体   中英

Chain inheritance java

i'm trying to implement a help vehicles society

but i'm first focusing on the vehicle part a vehicle can be a HeadQuarter or a Machine

A Machine is then specified into a Support Vehicle ( SV) or a Emergency Vehicle (EV)

EV and SV both extends Machine which extends Vehicle

Machines are particular because they can transport people, so i defined in the machine Class, a method Embark( String) that stores the string in the arraylist which is an attribute of Machine Class.

Then i went into the Society implementation, a Society have an ArrayList of vehicles ( whichs can be either a Machine( that specify into an EV or SV) or an HQ) but when i try to embark people in a SV, i can't access the Machine Methods

i don't really understand why can someone explain me please ? Thank you very much

here is the Society.java

import Vehicle.*;
import Vehicle.machine.SupportVehicle;
import Vehicle.machine.EmergencyVehicle;
import Vehicle.machine.*;

public class Society {
private ArrayList <Vehicle> Vehicles;
public void AddVehicle(Vehicle V1){
    this.Vehicles.add(V1);
}

public Society(){
    this.Vehicles = new ArrayList<Vehicle>();
}

public static void main(String[] args){

    Society S;
    S = new Society();

    SupportVehicle SV;

    SV =  new SupportVehicle();

    Vehicle V1; 
    V1 = new Vehicle();
    S.AddVehicle(V1);
    S.Vehicles.get(0).embark("bar"); // Embark is a Machine Method but i can't access it  : " The method Embark(string) is undefined for the type Vehicle

}

}

The problem is this " so i defined in the machine Class, a method Embark( String)" and you want to access that method from a vehicle.

Machine extends Vehicle so Vehicle does not have access to the machine's methods.

A possible solution is to add that method to the Vehicle class.

Another solution is to have a private ArrayList machines instead of private ArrayList Vehicles;

If you want to use ((Machine)S.Vehicles.get(0)).embark("bar"); you should first check the instance = "if S.Vehicles.get(0) instanceof Machine"

PS: the variables should start with lowercase =)

your problem is, that you "look" at your object through a Vehicle -Class "glasses", because you want to call the method on S.Vehicles.get(0) and Vehicles is a List of type Vehicle . In this case you are able to see only functions of the Vehicle -Class. To see the methods of Machine -Class you need to cast the object to Machine -Class first:

((Machine)S.Vehicles.get(0)).embark("bar");

embark( String str) is a method defined in Machine class an not Vehicle class. The compiler does not know that Society.Vehicles contains instances of Machine Class as it could contain instances of Vehicle Class itself or that of other extensions.

1) If you know that all the Vehicle instances in Society would be Machine Instances than just change the type of vehicles as follows

private ArrayList <Machine> Vehicles;

2) If Above case is not true than following would help

if(S.Vehicles.get(0) instanceof Machine)
{
    ((Machine)S.Vehicles.get(0)).embark("Something");
}

大家好,谢谢大家的回答,我都读了,发现问题是编译问题,我在vechile中添加了embark方法(字符串n1),并在机器中覆盖了它,以便在执行代码时一切顺利

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM