Java 如何从默认包导入类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2193226/
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
How to import a class from default package
提问by Micha? Ziober
Possible Duplicate: How to access java-classes in the default-package?
可能的重复:如何访问默认包中的 java 类?
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.javaand 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?
我正在使用 Eclipse 3.5,并且我已经创建了一个带有一些包结构和默认包的项目。我在默认包中有一个类 - Calculations.java,我想在任何包中使用该类(例如在 中com.company.calc
)。当我尝试使用默认包中的类时,它给了我一个编译器错误。它无法识别默认包中的类。问题出在哪儿?
Calculations.java - source code
Calculations.java - 源代码
public class Calculations {
native public int Calculate(int contextId);
native public double GetProgress(int contextId);
static {
System.loadLibrary("Calc");
}
}
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.
我不能将我的课程放在任何其他包中。这个类有一些在 Delphi 中实现的本地方法。如果我将该类放在任何文件夹中,我将不得不对我想避免的那个 DLL 进行更改(真的 - 我不能)。这就是为什么我将我的课程放在默认包中的原因。
采纳答案by McDowell
From the Java language spec:
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.
您必须通过反射或其他一些间接方法访问该类。
回答by Jon
Unfortunately, you can't import a class without it being in a package. This is one of the reasons it's highly discouraged. What I would try is a sort of proxy -- put your code into a package which anything can use, but if you really need something in the default package, make that a very simple class which forwards calls to the class with the real code. Or, even simpler, just have it extend.
不幸的是,您不能在没有包的情况下导入类。这是它非常不鼓励的原因之一。我要尝试的是一种代理——将您的代码放入一个任何东西都可以使用的包中,但是如果您真的需要默认包中的某些东西,请将其设为一个非常简单的类,它将调用转发到具有真实代码的类。或者,更简单的是,让它扩展。
To give an example:
举个例子:
import my.packaged.DefaultClass;
public class MyDefaultClass extends DefaultClass {}
package my.packaged.DefaultClass;
public class DefaultClass {
// Code here
}
回答by matt b
Classes in the default package cannot be imported by classes in packages. This is why you should not use the default package.
包中的类不能导入默认包中的类。这就是为什么你不应该使用默认包的原因。
回答by Harshal Malshe
I can give you this suggestion, As far as know from my C and C++ Programming experience, Once, when I had the same kinda problem, I solved it by changing the dll written structure in ".C" File by changing the name of the function which implemented the JNI native functionality. for example, If you would like to add your program in the package "com.mypackage", You change the prototype of the JNI implementing ".C" File's function/method to this one:
我可以给你这个建议,据我的 C 和 C++ 编程经验,有一次,当我遇到同样的问题时,我通过更改“.C”文件中的 dll 写入结构通过更改名称来解决它实现 JNI 本机功能的函数。例如,如果您想将您的程序添加到包“com.mypackage”中,您将实现“.C”文件的函数/方法的JNI的原型更改为:
JNIEXPORT jint JNICALL
Java_com_mypackage_Calculations_Calculate(JNIEnv *env, jobject obj, jint contextId)
{
//code goes here
}
JNIEXPORT jdouble JNICALL
Java_com_mypackage_Calculations_GetProgress(JNIEnv *env, jobject obj, jint contextId)
{
//code goes here
}
Since I am new to delphi, I can not guarantee you but will say this finally, (I learned few things after googling about Delphi and JNI): Ask those people (If you are not the one) who provided the Delphi implementation of the native code to change the function names to something like this:
由于我是 Delphi 的新手,我不能向你保证,但最后会说,(我在谷歌搜索 Delphi 和 JNI 后学到了一些东西):问问那些提供原生 Delphi 实现的人(如果你不是那个人)将函数名称更改为以下内容的代码:
function Java_com_mypackage_Calculations_Calculate(PEnv: PJNIEnv; Obj: JObject; contextId: JInt):JInt; {$IFDEF WIN32} stdcall; {$ENDIF} {$IFDEF LINUX} cdecl; {$ENDIF}
var
//Any variables you might be interested in
begin
//Some code goes here
end;
function Java_com_mypackage_Calculations_GetProgress(PEnv: PJNIEnv; Obj: JObject; contextId: JInt):JDouble; {$IFDEF WIN32} stdcall; {$ENDIF} {$IFDEF LINUX} cdecl; {$ENDIF}
var
//Any variables you might be interested in
begin
//Some code goes here
end;
But, A final advice: Although you (If you are the delphi programmer) or them will change the prototypes of these functions and recompile the dll file, once the dll file is compiled, you will not be able to change the package name of your "Java" file again & again. Because, this will again require you or them to change the prototypes of the functions in delphi with changed prefixes (e.g. JAVA_yourpackage_with_underscores_for_inner_packages_JavaFileName_MethodName)
但是,最后一个建议:虽然你(如果你是delphi程序员)或者他们会改变这些函数的原型并重新编译dll文件,但是一旦dll文件被编译,你将无法更改你的包名“Java”文件一次又一次。因为,这将再次要求您或他们使用更改的前缀(例如 JAVA_yourpackage_with_underscores_for_inner_packages_JavaFileName_MethodName)更改 delphi 中函数的原型
I think this solves the problem. Thanks and regards, Harshal Malshe
我认为这可以解决问题。谢谢和问候, Harshal Malshe
回答by Adnan Ghaffar
From some where I found below :-
从我在下面找到的一些地方:-
In fact, you can.
事实上,你可以。
Using reflections API you can access any class so far. At least I was able to :)
到目前为止,您可以使用反射 API 访问任何类。至少我能够:)
Class fooClass = Class.forName("FooBar");
Method fooMethod =
fooClass.getMethod("fooMethod", new Class[] { String.class });
String fooReturned =
(String) fooMethod.invoke(fooClass.newInstance(), "I did it");
回答by Andrey
Create "root" package (folder) in your project, for example.
package source; (.../path_to_project/source/)
Move YourClass.class into a source folder. (.../path_to_project/source/YourClass.class)
Import like this
import source.YourClass;
例如,在您的项目中创建“根”包(文件夹)。
包源;(.../path_to_project/source/)
将 YourClass.class 移动到源文件夹中。(.../path_to_project/source/YourClass.class)
像这样导入
导入 source.YourClass;
回答by Evgueni
- Create a new package.
- Move your files from the default package to the new one.
- 创建一个新包。
- 将您的文件从默认包移动到新包。
回答by Mohammad Banisaeid
There is a workaround for your problem. You can use reflection to achieve it.
您的问题有一个解决方法。您可以使用反射来实现它。
First, create an interfacefor your target class Calculatons
:
首先,为您的目标类创建一个接口Calculatons
:
package mypackage;
public interface CalculationsInterface {
int Calculate(int contextId);
double GetProgress(int contextId);
}
Next, make your target class implements that interface:
接下来,让您的目标类实现该接口:
public class Calculations implements mypackage.CalculationsInterface {
@Override
native public int Calculate(int contextId);
@Override
native public double GetProgress(int contextId);
static {
System.loadLibrary("Calc");
}
}
Finally, use reflectionto create an instance of Calculations
class and assign it to a variable of type CalculationsInterface
:
最后,使用反射创建Calculations
类的实例并将其分配给类型为 的变量CalculationsInterface
:
Class<?> calcClass = Class.forName("Calculations");
CalculationsInterface api = (CalculationsInterface)calcClass.newInstance();
// Use it
double res = api.GetProgress(10);
回答by Khushboo Kashyap
Create a new package And then move the classes of default package in new package and use those classes
创建一个新包然后将默认包的类移动到新包中并使用这些类