无法从静态上下文中引用。我这个代码应该怎么改?
class Box{
int length; //length width height
int width;
int height;
void setInfo(int l,int w,int h){
length=l;
width=w;
height=h;
}
int volume(){ //体积
return length*width*height;
}
int area(){ //表面积
return (length*width+width*height+length*height)*2;
}
public String toString(){
return "长:"+length+"\n"+"宽:"+width+"\n"+"高:"+height+"\n"+"体积:"+volume()+"\n"+"表面积:"+area();
}
void showInfo(){
System.out.println(toString()+"\n");
}
}
public class BoxTest{
public static void main (String [] args){
Box len=new Box();
Box wid=new Box();
Box hei=new Box();
Box.setInfo(123,321,213);
Box.showInfo();
}
}
编译后显示:
Boxtest.java:30:错误:无法从静态上下文中引用非静态 方法 setInfo(int,int,int)
Box.setInfo(123,321,213);
Boxtest.java:32:错误:无法从静态上下文中引用非静态 方法 showInfo()
Box.showInfo();
2个错误