简体   繁体   English

无法引用Java中的类

[英]Can not reference class in java

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

I want to make the mc as a reference. 我想以mc作为参考。 If objective-c is similar to this: 如果Objective-c与此类似:

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

}

Please help me. 请帮我。 Any answer is appreciate. 任何答案表示赞赏。 Thank you in advanced. 在此先感谢您。

In java we use reference variables instead of pointers. 在Java中,我们使用引用变量而不是指针。 See this and this 看到这个这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM