//This is a test filepublic class Test
{
public int add(int a , int b)
{
return a + b;
}
public int add(int a , int b , int c)
{
System.out.println(add(a,b));//这里搞不明白。为什么调用add(a,b)函数不写成这样test.add(a,b)?我的意思是:为什么不用对象调用?直接这样调用?怎么跟在main方法里的函数调用不一样啊?
return a + b + c;
}
public static void main(String[] args)
{

Test test = new Test();

int result = test.add(1, 2);
int result2 = test.add(1, 2, 3);
System.out.println(result);
System.out.println(result2);
}
}

解决方案 »

  1.   

    //This is a test filepublic class Test
    {
    public int add(int a , int b)
    {
    return a + b;
    }
    public int add(int a , int b , int c)
    {
    System.out.println(add(a,b));//这里搞不明白。为什么调用add(a,b)函数不写成这样test.add(a,b)?
    return a + b + c;
    }
    public static void main(String[] args)
    {

    Test test = new Test();

    int result = test.add(1, 2);
    int result2 = test.add(1, 2, 3);
    System.out.println(result);
    System.out.println(result2);
    }
    }
      

  2.   

    因为在整 个内 内部,有调用内部方法时,有一个隐含的 对象,即this,如果你懂c++的话,会明白这个在内部调用add(a,b),可以写成this.add(a,b),其中this表示当前正在调用方法的这个对象,即外部是哪个对象调用的,在内部就是哪个对象调用,如xx.add(a,b,c),在内部就可以理解为是xx.add(a,b)。
      

  3.   

    因为没有test这样东西... 如果把
    public int add(int a, int b, int c) {
    改成
    public staticint add(int a, int b, int c) {
    倒是可以
     Test.add
      

  4.   

    因为main方法是static修饰的静态方法,调用非静态方法需要对象调用,而在同一个类里非静态方法是可以直接调用的。
      

  5.   

    非静态方法,this内部调用,何来test
      

  6.   

    对象就是this 可省略
    看看基础
      

  7.   

    这是内部类,也可以加上this.text(a,b),表示的是在同一个类中