且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法在方法中访问调用类的变量?

更新时间:2022-04-15 00:54:42

您可以将 this 作为参数传递给第二个函数。

You can pass this as a parameter to the second function.

public class exClass {
   public int aVariable;

   public exClass()
   {
      othClass.aMethod(this);
   }
}

public class othClass{

   static void aMethod(exClass x)
   {
      x.aVariable = 0; //or call a setter if you want to keep the member private
   }
}