简体   繁体   English

链继承Java

[英]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 但我首先关注车辆部分,车辆可以是HeadQuarter或Machine

A Machine is then specified into a Support Vehicle ( SV) or a Emergency Vehicle (EV) 然后将机器指定为支援车辆(SV)或紧急车辆(EV)

EV and SV both extends Machine which extends Vehicle EV和SV都扩展了扩展车辆的机器

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. 机器之所以特别是因为它们可以运送人,所以我在机器类中定义了一种方法Embark(String),该方法将字符串存储在arraylist中,这是机器类的属性。

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 然后我进入协会的实现,一个协会有一个车辆的ArrayList(可以是Machine(指定为EV或SV)或HQ的机器),但是当我尝试将人放在SV中时,我不能访问机器方法

i don't really understand why can someone explain me please ? 我不太明白为什么有人可以向我解释? Thank you very much 非常感谢你

here is the Society.java 这是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. 问题是“所以我在机器类中定义了Embark(String)方法”,而您想从车辆访问该方法。

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. 一种可能的解决方案是将该方法添加到Vehicle类。

Another solution is to have a private ArrayList machines instead of private ArrayList Vehicles; 另一种解决方案是使用专用的ArrayList机器而不是专用的ArrayList Vehicles。

If you want to use ((Machine)S.Vehicles.get(0)).embark("bar"); 如果要使用((Machine)S.Vehicles.get(0))。embark(“ bar”); you should first check the instance = "if S.Vehicles.get(0) instanceof Machine" 您应该先检查instance =“ if S.Vehicles.get(0)instanceof Machine”

PS: the variables should start with lowercase =) PS:变量应以小写=开头

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 . 你的问题是,你“看”通过你的对象Vehicle -Class“眼镜”,因为你想打电话的方法S.Vehicles.get(0)Vehicles的类型的列表Vehicle In this case you are able to see only functions of the Vehicle -Class. 在这种情况下,您只能看到Vehicle -Class的功能。 To see the methods of Machine -Class you need to cast the object to Machine -Class first: 要查看Machine -Class的方法,您需要先将对象强制转换为Machine -Class:

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

embark( String str) is a method defined in Machine class an not Vehicle class. embark( String str)是在Machine类而不是Vehicle类中定义的方法。 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. 编译器不知道Society.Vehicles包含Machine Class的实例,因为它可能包含Vehicle Class本身或其他扩展的实例。

1) If you know that all the Vehicle instances in Society would be Machine Instances than just change the type of vehicles as follows 1)如果您知道协会中的所有Vehicle实例都将是Machine Instance,而不仅仅是更改车辆的类型,如下所示

private ArrayList <Machine> Vehicles;

2) If Above case is not true than following would help 2)如果上述情况不成立,那么以下将有所帮助

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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