当前位置: 首页 > 知识库问答 >
问题:

在java中使用继承时出现错误消息

荆学民
2023-03-14

我对java中的继承不熟悉,我有以下问题。我的基类是Plane,它的子类是PlaneComponent,PlaneComponent的子类是PasengerCom的。我的程序由11个类组成,当我忽略PasengerCom的类时,一切都是正确的。但是当我运行整个程序时,我收到了这个消息:at Plane。

飞机:

import java.util.*;

public class Plane
{
int cap;
int pl;

public Plane()
{
    String desc = "Plane Description";
    String title = "Boeing 747";
    Random rand = new Random();
    cap = rand.nextInt(100) + 51;       //initialize cap with 50<value<100
    **PassengerCompartment a8 = new PassengerCompartment();**
    pl = cap / a8.cap2;                 // pl = sum of Passenger Compartments
    if (cap % a8.cap2 != 0)
    {
        pl = pl - (cap % a8.cap2) + 1;
    }
}
public boolean ready_check()
{
    CargoBay a6 = new CargoBay();
    a6.ready_check();
    for (int i = 0 ; i < 3 ; i++)
    {
        EquipmentCompartment a7 = new EquipmentCompartment();
        a7.ready_check();
    }
    for(int i = 0; i < pl; i++)
    {
        PassengerCompartment a8 = new PassengerCompartment();
        a8.ready_check();
    }
    System.out.println("Plane OK!");
    return true;
}
public void process(String e)
{
    if(e == "SecurityEmployee")
    {
        SecurityEmployee a14 = new SecurityEmployee();
        a14.workOn();
    }
    if(e == "MaintenanceEmployee")
    {
        MaintenanceEmployee a15 = new MaintenanceEmployee();
        a15.workOn();
    }
    if(e == "CleaningEmployee")
    {
        CleaningEmployee a16 = new CleaningEmployee();
        a16.workOn();
    }
}

public void values()
{
    System.out.println("Would you like more info about the plane's capacity? type Y or N");
    Scanner input = new Scanner(System.in);
    String inp = input.nextLine();
    while (!(inp.equals("Y")) && !(inp.equals("N")))
    {
        System.out.println("Wrong input, please type again");
        inp = input.nextLine();
    }
    if ( inp.equals("Y"))
    {
        PassengerCompartment a8 = new PassengerCompartment();
        System.out.println("Plane's capacity: " + cap);
        System.out.println("PassComp's capacity: " + a8.cap2);
        System.out.println("Number ofPassenger Compartments " + pl);
    }
}

}

飞机组件:

**public class PlaneComponent extends Plane**
{
public boolean ready_check()
{
    return true;
}

public void process(String desc)
{
    Employee.workOn(desc);
}
}

乘客车厢:

import java.util.*;

public class PassengerCompartment extends PlaneComponent
{
Random rand = new Random();
boolean inner = rand.nextBoolean();
int cap2;
String desc;

public PassengerCompartment()
**{**
    desc = "Passenger Compartment";
    cap2 = rand.nextInt(50) + 21;       //initialize cap with 20<value<50
}
public boolean ready_check()
{
    System.out.println(desc);
    if (super.ready_check() == true)
    {
        System.out.println("Passenger Compartment OK!");
        if (inner == true)
        {
            desc = "Inner Compartment";
            System.out.println(desc);
            if (super.ready_check() == true)
            {
                System.out.println("Inner Compartment OK!");
            }
        }
    }
    return true;
}
public void process(String desc)
{
    super.process(desc);
}
}

共有2个答案

齐昆
2023-03-14

你的问题在于你的继承结构。

Plane的构造函数中,创建一个PassengerCompany实例<乘客舱扩展了飞机舱PlaneCompartment扩展“Plane”。所以,你在一个圆圈里。

我认为飞机舱扩展飞机没有意义。你所拥有的是一种“拥有-拥有”的关系。因此,< code>Plane有一个< code>PlaneCompartment,但两者都不互相扩展。

龚钧
2023-03-14
匿名用户

您有一种循环继承类型的情况( 可能 会创建StackOverflow异常):

您实例化< code > passenger compartment A8 = new passenger compartment();在< code>Plane的构造函数中。

PassengerCompany扩展了PlaneComponent,因此隐式调用PlaneComponent的构造函数(super()),该构造函数继承自Plane。在<code>Plane</code>的构造函数中,您有提到的<code>PassengerCompartment</code’的实例化等等…所以我强烈建议不要在所述类的构造函数中实例化继承自类的类。

我建议在这里阅读我的关于这个主题的问答。

 类似资料:
  • 问题内容: 在Eclipse中运行Ant构建时,出现以下错误消息,并且Ant构建失败。 错误消息: 运行javac.exe编译器时出错 停止构建的行: 有人遇到过类似的问题吗?还是有人知道这是怎么回事? 系统信息:Eclipse Helio,JDK 1.5 问题答案: 我有同样的问题,问题是,在Eclipse中,java.home是指JRE而不是JDK。我进行了更改,构建成功。您可以执行以下操作将

  • 我是Java的新手。谁能给我解释一下为什么会显示StackOverflowerr? 如果Generator类不是从类开始继承的,那么一切都正常,但为什么呢?

  • //为什么这是强制性的??? 当我做一个类派生从一个innerclass(Innerclass.后来我才知道,它需要包括一个构造函数采取封闭类引用为什么是这样?

  • 我使用Dropwizard框架创建了java应用程序,其中我使用了liquibase迁移。我能够从eclipse中运行“db migrate”命令,但同一命令在终端cli中失败。 很长时间以来,我一直在试图找出设置中的错误,但现在我需要一些液化专家的帮助。这是打印的错误日志。请让我知道-可能有什么问题。

  • 问题内容: 假设Java具有以下层次结构类: 这是C#中相同代码的(盲)重复: 当我执行Java代码时,我得到了C#返回的信息。 对我来说,C#的结果更有意义,因为引用B调用了它自己的方法。 Java设计者决定打印而不是打印的逻辑是什么?我的意思是,为什么引用B在C中使用覆盖方法?这种方法的优势是什么? 如何更改Java代码以像C#一样打印出来?我的意思是,我怎么教Java调用它使用的完全引用的方