我正在尝试运行一个方法,该方法将对两个对象的值求和。我正在得到"无法对非静态方法进行静态引用"。这是我的代码......
public class Main {
public static void main(String[] args) {
Circle obj1 = new Circle(10);
CircleTwo obj2 = new CircleTwo(20);
System.out.println(calculateRadiusSum(obj1, obj2));
}
public int calculateRadiusSum(Circle r1, CircleTwo r2) {
int R = r1.radius + r2.radius;
return R;
}
}
public class Circle {
int radius;
public Circle(int r) {
r = radius;
}
}
public class CircleTwo {
int radius;
public CircleTwo(int r) {
r = radius;
}
}
答案 0 :(得分:1)
calculateRadiusSum
也需要static
,因为它不会从Main
的实例调用:
将原型更改为
public static int calculateRadiusSum