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)
小弟不懂,请各位帮忙解决...谢谢.
----------------解决方案--------------------------------------------------------
怎么没有哪位帮忙呀.?
----------------解决方案--------------------------------------------------------
for( int j = 0; j < 4; j++)
lines[i] = new Line(i,i*i);
你这个应该是lines[j]吧,否则i<0或i>3都会出现数组越界的
----------------解决方案--------------------------------------------------------
数组越界.
----------------解决方案--------------------------------------------------------
for( int j = 0; j < 4; j++)
lines[i] = new Line(i,i*i); //起码应该循环四次,结果单步跟踪一次就结束 了
这里是循环了4次,是正确的。。。
----------------------------------------------------------------------------------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 45
at compos_inher.<init>(compos_inher.java:202)
at compos_inher.main(compos_inher.java:214)
出现这个的原因是:1:参数传了45大于你的实现长度。
2:for( int j = 0; j < 4; j++)
lines[i] = new Line(i,i*i);
传了你用是的i做为参数,所以你这个line数组一直就是lines[3](即后一个)初始化了,所以你前三个lines全是 nulll 所以不管你怎么传就会出现上面的异常。你可以这样打印一下就知道了:
public Test(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("<<<<<<<<" + lines[i]);
}
System.out.println("Combined Cons");
for (int k = 0; k < lines.length; k++) {
System.out.println(">>>>>>>>" + lines[k]);
}
}
3:在你的Test类中的dispose()也有问题。
public void dispose() {
System.out.println("CADsystem dispose");
t.dispose();
for (int j = lines.length; j > 0; j--) {
lines[j].dispose();
super.dispose();
}//
}
这个for有问题,应该从lines.lenght-1开始到j>=0结束,否则会出现下标越界/。
综上所述。如果你想正确运行,。就是这几个问题了~~~good luck~~
----------------解决方案--------------------------------------------------------
哦,不好意思了,是我看错了,其实我是想表示j而不是j....
----------------解决方案--------------------------------------------------------