-1

这个算法有什么错误?我该如何解决?Eclipse告诉我一个错误在main上

area c1 = new area();

import java.awt.Rectangle;

public class ClasseRettangolo {

    public class area {
        Rectangle box = new Rectangle(5 , 10, 15, 20);
        public double surface() {
            return ( box.getHeight() * box.getWidth());
        }
    }
    public class perimeter {
        Rectangle box = new Rectangle(5 , 10, 15, 20);
        public double outline() {
            return ((box.getHeight() + box.getWidth())* 2);
        }
    }
    public static void main(String[] args){
        area c1 = new area();
        perimeter c2 = new perimeter();

        System.out.println("The area of the Rectangle is: " + c1.surface());
        System.out.println("The perimeter of the Rectangle is: " + c2.outline());
    }
}
4

4 回答 4

3

要么使areaperimeterstatic

public static class area { .. }

public static class perimeter { .. }

或者你将不得不实例化ClasseRettangolo类,然后是嵌套的类。

new ClasseRettangolo().new area();
new ClasseRettangolo().new perimeter();

与问题无关

  • 您的类名不符合 Java 命名转换:它们应该以大写字母开头。
于 2013-11-02T11:52:37.430 回答
2

问题是您使用的是非静态内部类。看起来你是个新手,所以,暂时遵守这个简单的规则:每个类都应该在自己的 .java 文件中定义为顶级类。一旦您了解了简单类的工作原理,请阅读有关嵌套类的教程

此外,请尊重 Java 命名约定:类以大写字母开头。

于 2013-11-02T11:51:43.713 回答
0

将区域类和周界类设为静态类,如下所示

import java.awt.Rectangle;

public class ClasseRettangolo {

public static class area {
    Rectangle box = new Rectangle(5 , 10, 15, 20);
    public double surface() {
        return ( box.getHeight() * box.getWidth());
    }
}
public static class perimeter {
    Rectangle box = new Rectangle(5 , 10, 15, 20);
    public double outline() {
        return ((box.getHeight() + box.getWidth())* 2);
    }
}
public static void main(String[] args){
    area c1 = new area();
    perimeter c2 = new perimeter();

    System.out.println("The area of the Rectangle is: " + c1.surface());
    System.out.println("The perimeter of the Rectangle is: " + c2.outline());
}
}

从oracle 文档中阅读更多关于静态内部类的信息

于 2013-11-02T11:51:54.183 回答
0

areaperimeter属于类ClasseRettangolo。因为它们不是静态的而是内部类,所以如果没有外部类的实例就不可能创建它们的实例。即使那样名字也不会area,它会ClasseRettangolo.area

于 2013-11-02T11:51:55.060 回答