这是我的第一堂课叫做班级圈:
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
答案 0 :(得分: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
。
答案 1 :(得分:1)
进行操作时,区域未分配:
c3 = c1.area(c2);
您需要先进行GeArea()调用,然后才能使用该类的区域字段。
例如:
circle circle1 = new circle(4,9,6);
circle circle2 = new circle(4,7,6);
circle2.area = c1.getArea();
假设您尝试分配的c3 var已被实例化为圆圈。
答案 2 :(得分:0)
你只是忘了一个右括号
if (this.area > other.area)
{
return other;
} //You forgot this brace and confused the compiler
else
{
return this;
}