1
class MyClass {
   int value;
}
public MyClass test(){
   MyClass mc;
   myMethod(mc, 100);
   if (mc!=null) {
      mc = mc.value;
      return mc;
   }
   return mc;
}
public int myMethod(MyClass mc, int a) {
   mc.value = a + 10;
   return mc;
}

我想让mc作为参考。如果objective-c与此类似:

public MyClass test(){
   MyClass *mc;
   myMethod(&mc, 100);

}

请帮我。任何答案都值得赞赏。提前谢谢你。

4

1 回答 1

1

在java中,我们使用引用变量而不是指针。看到这个这个

class MyClass 
{
   int value;

/*All methods have to belong to a class*/

public MyClass test()
{
 /*MyClass mc; you don't need to pass this to assign value the value will be 
 assigned to the data members of the object(reference variable) which calls the
 function*/       

MyClass mc = myMethod(100);
return mc;
}

public MyClass myMethod(int a) 
{
   value = a + 10;
   return this; //This 'this' will return the current object
}
}
于 2013-04-01T10:15:25.633 回答