【问题标题】:How can I reference the same class variable in different classes?如何在不同的类中引用相同的类变量?
【发布时间】:2025-12-24 14:25:10
【问题描述】:

我有一个“PowerManager.WakeLock”类型的变量“wl”。我需要使用相同的“wl”变量,而不是不同类中的不同对象。这个可以吗?

 public PowerManager.WakeLock wl; 
 // This variable needs to be used like a switch in other classes. 
 // In one class its turned on, in another class I need to call 
 // a method .Release(), to turn it off, 
 // but it has to refer to that same "wl" variable.

NotificationReceiver CLass - “持有电源管理器

     public class NotificationReceiver extends Service {
public PowerManager powerManager = (PowerManager) 
      getSystemService(POWER_SERVICE);
public static PowerManager.WakeLock wl = 
       powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
       "My:WakelockTag"); //Non-static field 'powerManager' cannot be referenced from a static context


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    wl.acquire();


    return START_NOT_STICKY;

}

  //--------------------------------------------------------------------

我需要在其中使用该变量的 MainActivity 类

public class MainActivity extends AppCompatActivity {
       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.start_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            Intent serviceIntent = new Intent (MainActivity.this, ExampleService.class);

            startService(serviceIntent);





            NotificationReceiver nn = new NotificationReceiver();// Here I make a new object to access the .aquire() method 

            nn.wl.acquire();

        }
    });

【问题讨论】:

    标签: java android


    【解决方案1】:

    在构造类时或在使用它的各个函数中传递它。

    【讨论】:

      【解决方案2】:

      可以通过将变量设为静态来完成。您可以通过 ClassName.variable 访问它。 (它只是一个类的字段,但可以从你的所有类中访问)

      【讨论】:

      • 这是不正确的。静态变量对同一类的所有实例都是通用的,而不是不同的类。
      • 是的,但是作者想要一个实例,他可以在他的所有课程中使用(嗯,我是这样理解的),如果他公开,他可以使用 ClassName.variable 来访问它.
      • @SyedAhmedJamil 是的。我就是这么想的。
      • @krymus42 我明白了。抱歉没有考虑清楚。是的,这是可以做到的。我认为您也必须在回答中详细说明这一点。但这取决于你。
      • @LinS 是的,但 krymus42 想说的是,您可以使用语法 ClassName.variableName 在任何地方访问类公共静态变量。其中 ClassName 是您声明静态变量的类的名称
      【解决方案3】:

      考虑Singleton 模式:

      class LockHolder {
      
        public static final LockHolder INSTANCE = new LockHolder();
      
        private final PowerManager.WakeLock wakeLock;
      
        // constructor is private so that noone can create new instances
        private LockHolder() { 
          this.wakeLock = new WakeLock();
        }
      
        // accessor method is safer than exposing the variable itself;
        // you may eventually end up doing more things here
        public PowerManager.WakeLock wakeLock() {
          return wakeLock;
        }
      }
      

      LockHolder 是一个Singleton,因为这个类只存在一个实例。

      你按如下方式使用它:

      PowerManager.WakeLock lock = LockHolder.INSTANCE.wakeLock();
      

      【讨论】:

      • 尝试使用您的建议但得到 -"'android.os.PowerManager' 不是封闭类"构造函数正在抛出这个。你不是说“构造函数的私有 LockHolder() 吗?因为那是你给的类的名称?谢谢
      • 你是对的LockHolder,谢谢!更正了代码。至于not an enclosing class,那就另当别论了,试试这个*.com/questions/31104476/…
      最近更新 更多