错误找不到符号-Java类和方法

时间:2016-03-08 17:08:15

标签: java class methods

什么样的人, 我一直收到这个错误,在尝试编译我教授给我们的驱动程序代码时找不到符号Circle aCircle = new Circle();。我想知道是不是因为我还没有将它添加到我的circle.java方法中。这是圈子驱动程序。

package lab7;

public class CircleDriver {

      public static void main(String[] args) {

            Circle aCircle = new Circle();

            aCircle.setColor("green");
            aCircle.setRadius(10);
            aCircle.display();

            Double circleArea = aCircle.computeArea();
            Double circumference = aCircle.computeCircumference();

            System.out.println("circle area: " + circleArea);
            System.out.println("circle circumference: " + circumference);
            System.out.println();
          }

}

这是我的圈子方法。

public class Circle {

    private String color;
    private int radius;

    public Circle(String color, int radius) {
        this.color = color;
        this.radius = radius;
    }

    public Circle() {
     Circle aCircle = new Circle();

    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void display() {
        System.out.println("I am a circle");
        System.out.println("My color is " + color);
        System.out.println("My radius is " + radius);
    }

    public double computeArea() {
        return (Math.PI * Math.pow(radius, 2));
    }

    public double computeCircumference() {
        return (2 * Math.PI * radius);
    }

}

2 个答案:

答案 0 :(得分:1)

调用圆时需要调用super()构造函数。当你打电话

Circle aCircle = new Circle();

您正尝试在本地方面初始化一个圆圈。我想你正在尝试继承已经在java中的Circle类。

将圆形构造函数保留为

public Circle() {}

理论上应该可以实例化你的课程。

答案 1 :(得分:0)

使用此代码。你在构造函数中有错误。我希望它能解决你的问题。

Circle.java 中,而不是

public Circle() {
 Circle aCircle = new Circle();
}

使用此代码

public Circle() {
    super();
    // TODO Auto-generated constructor stub
}

<强> CircleDriver.java

public class CircleDriver {
    public static void main(String[] args) {
        Circle aCircle = new Circle();
        aCircle.setColor("green");
        aCircle.setRadius(10);
        aCircle.display();
        Double circleArea = aCircle.computeArea();
        Double circumference = aCircle.computeCircumference();
        System.out.println("circle area: " + circleArea);
        System.out.println("circle circumference: " + circumference);
        System.out.println();
    }
}

<强> Circle.java

public class Circle {

    private String color;
    private int radius;

    public Circle() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Circle(String color, int radius) {
        super();
        this.color = color;
        this.radius = radius;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void display() {
        System.out.println("I am a circle");
        System.out.println("My color is " + color);
        System.out.println("My radius is " + radius);
    }

    public double computeArea() {
        return (Math.PI * Math.pow(radius, 2));
    }

    public double computeCircumference() {
        return (2 * Math.PI * radius);
    }

}

<强>输出:

I am a circle
My color is green
My radius is 10
circle area: 314.1592653589793
circle circumference: 62.83185307179586