簡體   English   中英

如何訪問私有類中存在的公共方法

[英]How to access public method present inside private class

我的代碼:

public class location
{

private class MyPhoneStateListener extends PhoneStateListener
    {
       //Get the Signal strength from the provider, each time there is an update 
      @Override
      public void onSignalStrengthsChanged(SignalStrength signalStrength)
      {

      }
/*some text*/

}

如何從“location”類調用“onSignalStrengthsChanged”方法。

您需要創建一個新的MyPhoneStateListener實例並在此實例上調用該方法。

例如:

public class location {

    private class MyPhoneStateListener extends PhoneStateListener {
      //Get the Signal strength from the provider, each time there is an update 
      @Override
      public void onSignalStrengthsChanged(SignalStrength signalStrength)
      {

      }
      /*some text*/

    }

    public void doSomething() {
        PhoneStateListener listener = new MyPhoneStateListener();
        listener.onSignalStrenghtsChanged(...);
    }
}

請注意,您只能在location類中創建MyPhoneStateListener實例,因為您MyPhoneStateListener類定義為private。

另請注意, doSomething()屬於location

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM