我对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);
}
}
你的问题在于你的继承结构。
在Plane
的构造函数中,创建一个PassengerCompany
实例<乘客舱扩展了飞机舱PlaneCompartment
扩展“Plane”。所以,你在一个圆圈里。
我认为飞机舱扩展飞机没有意义。你所拥有的是一种“拥有-拥有”的关系。因此,< code>Plane有一个< code>PlaneCompartment,但两者都不互相扩展。
您有一种循环继承类型的情况(
可能
会创建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中失败。 很长时间以来,我一直在试图找出设置中的错误,但现在我需要一些液化专家的帮助。这是打印的错误日志。请让我知道-可能有什么问题。
我似乎找不到有关自定义异常类的太多信息。 我所知道的 您可以声明您的自定义错误类并让它从继承,因此可以是d: 这允许您使用以下方法提升它: 稍后,在救援时获取该消息 我不知道的是 我想给我的异常提供一些自定义字段,但我想从父类继承<code>消息@message不是exception类的实例变量,因此我担心我的继承将不起作用。 有人能告诉我更多细节吗?如何使用<code>对象 然后: 得到: 它会