将对象作为方法中的参数传递

时间:2015-01-06 15:36:13

标签: java oop methods

我正在尝试运行一个方法,该方法将对两个对象的值求和。我正在得到"无法对非静态方法进行静态引用"。这是我的代码......

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;
    }

}

1 个答案:

答案 0 :(得分:1)

calculateRadiusSum也需要static,因为它不会从Main的实例调用:

将原型更改为

public static int calculateRadiusSum