import java.util.*;
class Shape{
Shape(int i){
System.out.println("Shape Cons");
}
void dispose(){
System.out.println("shape dispose");
}
}
class Triangle extends Shape{
Triangle( int i){
super(i);
System.out.println("Triangle Cons");
}
void dispose(){
System.out.println("Triangle dispose");
}
}
class Line extends Shape{
private int start,end;
Line( int start, int end ){
super(start);
this.start = start;
this.end = end;
System.out.println("Draw Line" + start + ", " + end);
}
void dispose(){
System.out.println("Erasing Line " + start + " ," + end);
}
}
public class compos_inher extends Shape{
private Triangle t;
private Line[] lines = new Line[4];
public compos_inher( int i){
super( i + 1 );
t = new Triangle(1);
for( int j = 0; j < 4; j++)
lines[i] = new Line(i,i*i); //起码应该循环四次,结果单步跟踪一次就结束 了
System.out.println("Combined Cons");
}
public void dispose(){
System.out.println("CADsystem dispose");
t.dispose();
for( int j = lines.length; j > 0; j--){
lines[j].dispose();
super.dispose();
}//
}
public static void main(String[] args){
compos_inher x = new compos_inher(45);
try{
}finally{
x.dispose();
}
}
}
//编译器就打印了如下
Shape Cons
Shape Cons
Triangle Cons
Shape Cons
Draw Line45, 2025
并跑出异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 45
at compos_inher.<init>(compos_inher.java:202)
at compos_inher.main(compos_inher.java:214)
小弟不懂,请各位帮忙解决...谢谢.