报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTestb
报错原因:在一个类中写了一个public void maxArea ()方法和一个main()方法,在main()方法中直接调用maxArea()方法就出现如题的错误。
解决方法,有两种:
方法一、maArea()定义时加上修饰符static,即变为public static void maxArea(),即可在main()方法中正确调用。
public class ShapeTestb {
public static void main(String[] args) {
Shape[]shapes = new Shape[2];
maxArea(shapes);
}
//在main()中编译错误
//public void maxArea(Shape[] shapes) {
//在main()中编译正确
public static void maxArea(Shape[]shapes) {
…………
}
}
方法二、先实例化类,然后通过对象调用maxArea()方法也行。
public class ShapeTestb {
public static void main(String[] args) {
Shape[] shapes = new Shape[2];
ShapeTestb st = new ShapeTestb();
st.maxArea(shapes);
}
public void maxArea(Shape[] shapes) {
…………
}
}