Java 如何调用保存在其他包下的类的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3861716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:57:26  来源:igfitidea点击:

how to call a Method of a class kept under other package

javaandroid

提问by Ankit

I am a beginner in Android Developing. Can any1 please guide me how to call a Method of a class kept under other package.

我是 Android 开发的初学者。谁能指导我如何调用保存在其他包下的类的方法。

Like class A in Package 1 calls a method in Class B of Package 2 which returns An array or object.

就像包 1 中的类 A 调用包 2 的类 B 中的方法一样,该方法返回一个数组或对象。

Do i have to create an Intent for that?? actually i have to gather all information in 1 class from different classes kept under different packages.

我是否必须为此创建一个意图?实际上,我必须从保存在不同包下的不同类中收集 1 个类中的所有信息。

Thanks in advance.

提前致谢。

package com.xyz.Master;


import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

public class PhoneInfo extends Activity {

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();  
public int cellID, lac,mcc,mnc;
public String imei,manufacturer,model,product;
String[] phoneInfo;
int[] phoneLocationInfo;    

public String[] getHandsetInfo()
{
    manufacturer = Build.MANUFACTURER;
    model = Build.MODEL;
    product = Build.PRODUCT;
    imei=tm.getDeviceId();        

    String softwareVersion = tm.getDeviceSoftwareVersion();

    phoneInfo = new String[5];
    phoneInfo[0]=imei;
    phoneInfo[1]=product;
    phoneInfo[2]=model;
    phoneInfo[3]=manufacturer;
    phoneInfo[4]=softwareVersion;
    return phoneInfo;

}
public int[] getHandsetLocationInfo()
{
    phoneLocationInfo= new int[4];
    String networkOperator = tm.getNetworkOperator();
     if (networkOperator != null) {
            mcc = Integer.parseInt(networkOperator.substring(0, 3));
            mnc = Integer.parseInt(networkOperator.substring(3));
      }
     CellLocation.requestLocationUpdate(); 
     cellID = location.getCid();
     lac = location.getLac();

     phoneLocationInfo[0]=cellID;
     phoneLocationInfo[1]=lac;
     phoneLocationInfo[2]=mcc;
     phoneLocationInfo[3]=mnc;

     return phoneLocationInfo;

}
}

I want to call above methods from other class and get these arrays. How to do that, is there any error in above code??

我想从其他类调用上述方法并获取这些数组。怎么做,上面的代码有没有错误??

回答by bhups

Just import other package and instantiate the class B and call the function.

只需导入其他包并实例化类 B 并调用该函数。

package Package1;
import Package2;
Class A {
    void callClassBFunction(){
        B b = new B();  //In Package 1 we are instantiating B, however the reference is missing, it should be B b = new B();
        b.callFunction();
}
package Package2;
public class B { //class B should be public
    Object callFunction(){
        //do something and return object
    }
}

回答by Andreas Dolk

Assuming, we're talking about the Java package, then we have several rules for calling methods on classes in other packages. To keep it simple, this works:

假设我们谈论的是 Java package,那么我们有几个规则来调用其他包中的类的方法。为简单起见,这有效:

package com.example.one;
public class ArrayProvider {
  public String[] getArray() {
    return new String{"I'm ","inside ", "an ", "array "};
  }
  public static Object getObject() {
    return new String{"I'm ","inside ", "an ", "array "};
  }
}

Now your code to access the methods of class ArrayProviderfrom the other package:

现在你的代码ArrayProvider从另一个包访问类的方法:

package my.namespace.app;
import com.example.one.ArrayProvider;      // import the other class

public class MyClass {

  public static void main(String[] args) {

    // to access the static method
    Object result1 = ArrayProvider.getObject(); 

    // to access "the other" method
    ArrayProvider provider = new ArrayProvider();
    String[] result2 = provider.getArray();
  }
}


Further Reading

进一步阅读

  • Java 教程(您的问题针对基本的 Java 知识)