【问题标题】:non-static method cannot be referenced by static context [duplicate]静态上下文不能引用非静态方法[重复]
【发布时间】: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


【解决方案1】:

问题是 myLine() 方法,你应该改变:

public void myLine(int size) 

到

public static void myLine(int size)  

静态方法的管理方式与常规方法不同,因为它们位于静态上下文中。

这里有一些信息可以帮助您更好地理解这一点:

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

顺便说一句,如果您想要更好、更快的响应,您应该尽可能删除标记的源代码。

【讨论】:

  • 简单解释一下,静态方法是属于整个类的方法。即使您没有实例化new myLine(),它也可以供您调用。非静态方法属于特定对象 - myLine 的一个实例。在您实际实例化 new 对象之前,它们不存在。这就是您收到错误的原因 - 您试图在尚未创建时引用属于 myLine 实例的方法。
  • 对不起。下次会记住这一点,谢谢
【解决方案2】:

您收到编译错误是因为您试图从 driver#main(String[])(这是一个静态方法)中调用 driver#myLine(int)(这不是一个静态方法)。简单的解决方法是将driver#myLine(int)的方法签名更改为:

public static void myLine( int size ){

【讨论】:

  • 这样做又得到 3 个错误:/home/miguel/School/project3/driver.java:30: error: class, interface, or enum expected } ^ /home/miguel/School/project3 /driver.java:31:错误:预期类、接口或枚举 public static void myLine(int size){ ^ /home/miguel/School/project3/driver.java:33:错误:预期类、接口或枚举} ^ 3 个错误 [在 5.2 秒内完成,退出代码为 1]
【解决方案3】:

你的命名约定是错误的!

你的关闭块,方法类大括号不正确!

删除了注释代码以提高可读性。

如果你想从静态方法调用它,你还需要将你的 myLine 方法更改为类方法。

您可以如下更改您的代码

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

}

public static void myLine(int size) {
    myLine(size);
}

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2023-03-22
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多