【发布时间】:2015-06-02 05:32:59
【问题描述】:
所以我有一个程序,我正在尝试运行它绘制一条线,正方形,它接受命令行参数说 l 5 s 4 c 6。这样我使用2Darray 绘制形状。在我的程序中,我有一个驱动程序类、一个char2D 类(我的处理2darray 的类)和myLine。 myLine 使用继承派生自 char2D。
我的问题是: 为什么我得到一个编译错误,不能从静态上下文中引用?我是否必须将解析命令行参数的 switch 语句移动到不同的方法?如果是这样,除了将语句切入新方法之外,我还需要做其他事情吗?
这是我的编译错误。
/home/miguel/School/project3 v2/driver.java:11: 错误:无法从静态上下文引用非静态方法 myLine(int) 我的线(大小1); ^
这是我的 驱动程序 类(忽略注释掉的部分,因为我重写了我的程序):
public class driver {
public static void main(String[] args ){
String type = args[0];
int size;
switch (type.toLowerCase().charAt( 0 )) {
case 'l':
int size1;
size1 = Integer.parseInt( args[1] );
myLine( size1 );
break;
// case 's' :
// size2 = Integer.parseInt(args[1]);
// mySquare( size );
// break;
// case 'c' :
// size3 = Integer.parseInt(args[1]);
// myCube( size );
// break;
}
}
}
}
public static void myLine( int size ){
myLine( size );
}
// System.out.print( "*"); // don't need to print in here
//myLine(int size);
// public int mySquare( int size ) {
//for (i = 0; i < size ; i++)
// mySquare( size );
}
// public void myCube( int size ){ // z will be x but only have the length of it
//z = size + .5
// myCube(size )
// }
这是我的 char2D 类
public class char2D {
protected int width; protected int height; protected char char2D[][];
public char2D( int width, int height ){ //line
this.width = width;
this.height = height;
char2D = new array[height][width];
}
public void grow( int width, int height ){ //note width and height are changing in this method
this.height += height;
this.width += width;
char2D = new array[this.height][this.width];
}
public void plot( int x1, int y1 ){
char2D[y1][x1] = '*';
}
public static toString( char2D )
return char2D.toString();
}
这是我的 myLine 课程:
public class myLine extends char2D{
private int size1;
public myLine(int size1){
super( size1, 1 );
this.size1 = size1;
plotLine();
}
private void plotLine(){
for( int i = 0; i < size1 ; i++){
plot( 0 , i );
}
}
}
【问题讨论】:
-
将
public void myLine( int size )更改为public static void myLine( int size ) -
我打算让你对其中的一两个编译错误有疑问,但你有一大堆这样的问题,这使得格式化你的代码困难。请发布可编译代码。在这一点上,我并不完全清楚你在用
myLine做什么,因为它只会调用自己。
标签: java arrays inheritance