2016-02-28 17 views
1

我是java中的新继承人,我遇到了以下问题。我的基类是Plane,它的子类是PlaneComponent,PlaneComponent的子类是PassengerCompartment。我的课程由11个班级组成,当我忽略PassengerCompartment班级的权利时。但是当我运行整个程序时,我收到以下消息:at Plane.<init>(Plane.java:14) at PlaneComponent.<init>(PlaneComponent.java:1) at PassengerCompartment.<init>(PassengerCompartment.java:11) 这是反复打印很多次,我看不到错误的第一行。错误消息中包含的行是粗体行(这里在** **之间打印)。在java中使用继承时的错误消息

飞机:

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); 
    } 
} 

} 

PlaneComponent:

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

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

PassengerCompartment:

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); 
} 
} 
+1

这个错误信息打印了很多次,我看不到第一行stacktrace。你能帮我找到一种方法来查看整个堆栈跟踪吗? –

+1

另外,在比较字符串时,使用等号方法 示例e.equals(“SecurityEmployee”) –

+1

不需要'== true',只需使用if(bool)'。 – Idos

回答

0

你的问题是你的继承结构。

Plane的构造函数中,您创建了一个PassengerCompartment的实例。 PassengerCompartment延伸PlaneCompartmentPlaneCompartment延伸`平面'。所以,你在一个圈子里。

我不认为PlaneCompartment扩展Plane是有意义的。 你有什么是'有'的关系。所以,Plane有一个PlaneCompartment但都不相互延伸。

+0

我刚刚从PlaneComponent中删除了“extends Plane”,一切正常。非常感谢! –

2

你有一个圆形的继承实物-的局势(这可能创建StackOverflow例外):

您在Plane的构造函数中实例化PassengerCompartment a8 = new PassengerCompartment();

PassengerCompartmentextendsPlaneComponent,所以PlaneComponent构造被称为隐式super()),其从Plane继承。在Plane的构造函数中,你有提到的实例PassengerCompartment等等......所以我强烈建议而不是实例化在你的类的构造函数中继承的类。

我会推荐阅读我的关于这个话题的问答here