如何从默认包导入类 [英] How to import a class from default package

查看:37
本文介绍了如何从默认包导入类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:如何在默认包?

<小时>

我正在使用 Eclipse 3.5,并且我创建了一个项目,其中包含一些包结构和默认包.我在默认包中有一个类 - Calculations.java,我想在任何包中使用该类(例如在 com.company.calc 中).当我尝试使用默认包中的类时,它给了我一个编译器错误.它无法识别默认包中的类.问题出在哪里?


I am using Eclipse 3.5 and I have created a project with some package structure along with the default package. I have one class in default package - Calculations.java and I want to make the use of that class in any of the package (for instance in com.company.calc). When I try to make the use of the class which is in the default package, it's giving me a compiler error. It's not able to recognise the class in default package. Where is the problem?

Calculations.java - 源代码

Calculations.java - source code

public class Calculations {
    native public int Calculate(int contextId);
    native public double GetProgress(int contextId);
    static  {
        System.loadLibrary("Calc");
    }
}

我不能将我的课程放在任何其他包中.这个类有一些在 Delphi 中实现的本地方法.如果我将该类放在任何文件夹中,我将不得不对我想避免的那个 DLL 进行更改(真的 - 我不能).这就是为什么我把我的类放在默认包中.

I can't put my class in any other package. This class has some native methods which are implemented in Delphi. If I put that class in any of the folders, I will have to make changes to that DLL which I want to avoid (really - I can not). That's why I put my class in the default package.

推荐答案

来自 Java 语言规范:

从未命名的包中导入类型是编译时错误.

It is a compile time error to import a type from the unnamed package.

您必须通过反射或其他一些间接方法访问该类.

You'll have to access the class via reflection or some other indirect method.

这篇关于如何从默认包导入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆