在Eclipse中,跨包调用方法有以下两种方式:
import com.example.otherpackage.OtherClass; public class MyClass { public static void main(String[] args) { OtherClass.otherMethod(); // 调用OtherClass类的静态方法 } }
import com.example.otherpackage.OtherClass; public class MyClass { public static void main(String[] args) { OtherClass otherObj = new OtherClass(); otherObj.otherMethod(); // 调用OtherClass类的实例方法 } }
需要注意的是,为了能够跨包调用方法,被调用方法所在的类和方法需要是public可见性。另外,如果被调用方法位于不同的包中,还需要在被调用方法所在的包中将该方法声明为public。