繁体   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