Java通过引用传递vs传递值的麻烦

时间:2011-09-22 04:20:30

标签: java

为什么我通过引用示例obj1.add200really在传递中出现错误带有下划线

public class Test {

    private int number;

    Test(){
        number = 1;
    }

    public static void main(String[] args) {
        Test obj1 = new Test();
        System.out.println("the number is " + obj1.number);
        System.out.println("the number 1 plus 200 is " + obj1.add200(obj1.number));
        System.out.println("while the number is still " + obj1.number);
        System.out.println("\n");
        System.out.println("the number is " + obj1.number);
        System.out.println("the number 1 plus 200 is " + obj1.add200really(obj1.number));
        System.out.println("while the number is still " + obj1.number);
    }


int add200(int somenumber){
    somenumber = somenumber + 200;
    return somenumber;
}
int add200really(Test myobj){
    myobj.number = 999;
    return myobj.number;
}
}

2 个答案:

答案 0 :(得分:1)

使用obj1.add200really(obj1);

答案 1 :(得分:0)

因为您没有方法add200really(int)

您的方法add200really()需要一个对象。您正尝试使用int

进行调用