-3

这是我的第一堂课,叫做课堂圈:

public class circle
{
   //circle class begins
   //declaring variables 
   public double circle1;
   public double circle2;
   public double circle3;
   public double Xvalue;
   public double Yvalue;
   public double radius;
   private double area;

   //Constructor
   public circle(int x,int y,int r)
   {//constructor begins
       Xvalue = x;
       Yvalue = y;
       radius = r;
   }//constructor ends

   //method that gets the area of a circle       
   public double getArea ()
   {//method getArea begins

      area = (3.14*(this.radius * this.radius));
      return area;
   }//getArea ends

   public static smaller (circle other)
   {
      if (this.area > other.area)
      {
         return other;
      else 
      {
         return this;
      }

      //I'm not sure what to return here. it gives me an error( I want to return a circle)
    }
}//class ends
}

这是我的测试类:

public class tester
{//tester begins
  public static void main(String args [])
  {

        circle circle1 = new circle(4,9,4);
        circle circle2 = new circle(4,7,6);
        c3 = c1.area(c2);

        System.out.println(circle1.getArea());
        //System.out.println(
  }
}//class tester ends
4

3 回答 3

2

smaller方法应该有一个返回类型。关键字也this不能在static方法中使用。即该方法将无法访问Circle. 考虑到方法名称的含义,这是有道理的smaller——它将当前实例Circle与传入的另一个实例进行比较。

public Circle smaller(circle other) {
   if (this.area > other.area) {
    return other;
   } else {
    return this;
   }
}

要使用:

Circle smallerCircle = circle1.smaller(circle2);

Aside:Java 命名约定表明类名以大写字母开头Circle

于 2013-04-18T01:01:04.763 回答
1

进行操作时未分配区域:

 c3 = c1.area(c2);

您需要先调用 GeArea(),然后才能使用类的 area 字段。

例如:

circle circle1 = new circle(4,9,6);

circle circle2 = new circle(4,7,6);
circle2.area = c1.getArea();

那是假设您尝试分配的 c3 var 已被实例化为一个圆圈。

于 2013-04-18T01:01:43.093 回答
0

你只是忘记了一个右括号

if (this.area > other.area)
{
    return other;
} //You forgot this brace and confused the compiler
else 
{
    return this;
}
于 2013-04-18T01:01:44.813 回答