Java 导入不同目录下的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25253758/
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
import class in different directory
提问by shanwu
Actually, I am trying to finish this practice in "Think in Java" for self-learning purpose --
实际上,为了自学目的,我正在尝试在“Think in Java”中完成此练习-
Exercise 6: (2) Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return.
练习 6:(2)在它自己的包中创建一个具有至少一种方法的接口。在单独的包中创建一个类。添加一个实现接口的受保护的内部类。在第三个包中,从您的类继承,并在方法内部返回受保护内部类的对象,并在返回期间向上转换到接口。
so I created a class named IgetResult.java under directory "a" which has a IIGetResult Interface.
所以我在目录“a”下创建了一个名为 IgetResult.java 的类,它有一个 IIGetResult 接口。
interface IIGetResult {
String getResult();
}
public class IgetResult {
}
then I create another class in another directory -- directory b
然后我在另一个目录中创建另一个类——目录 b
import a.IgetResult.IIGetResult;
public class PracticeClass {
protected class inner implements IIGetResult {
@Override
String getResult(){ return "result";}
}
public static void main(String[] args) {
System.out.println("practice start");
}
}
In the final step, I compile the two java classes with command:
在最后一步,我使用命令编译两个 java 类:
# javac a/.java b/.java
# javac a/ .java b/.java
and get the following error:
并得到以下错误:
./a/IgetResult.java:1: duplicate class: IIGetResult
interface IIGetResult {
^
./a/IgetResult.java:4: duplicate class: IgetResult
public class IgetResult {
^
b/PracticeClass.java:1: cannot access a.IgetResult
bad class file: ./a/IgetResult.java
file does not contain class a.IgetResult
Please remove or make sure it appears in the correct subdirectory of the classpath.
import a.IgetResult.IIGetResult;
^
Please teach me go through this practice, thanks in advance.
请教我通过这个练习,在此先感谢。
采纳答案by dimoniy
As per the quote:
根据报价:
Create an interface with at least one method, in its own package.
在它自己的包中创建一个具有至少一种方法的接口。
So we create IGetResult.java
file in folder a
:
所以我们IGetResult.java
在文件夹中创建文件a
:
package a;
public interface IGetResult {
String getResult();
}
Create a class in a separate package. Add a protected inner class that implements the interface.
在单独的包中创建一个类。添加一个实现接口的受保护的内部类。
Now we create a class in a separate package (folder), with inner class which implements the interface:
现在我们在一个单独的包(文件夹)中创建一个类,内部类实现了接口:
package b;
import a.IGetResult;
public class InnterTest {
protected class GetResultImpl implements IGetResult {
@Override
String getResult() { return "result"; }
}
}
In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return
在第三个包中,从您的类继承,并在方法内部返回受保护内部类的对象,在返回期间向上转换到接口
So now we create a sub-class of InnerTest
class in third separate package:
所以现在我们InnerTest
在第三个单独的包中创建一个类的子类:
package c;
import a.IGetResult;
import b.InnterTest;
public class InnerTestSubclass extends InnerTest {
public IGetResult getResultClass() {
//Up-casting happens automatically since GetResultImpl is sub-class of IGetResult
return new GetResultImpl();
}
}
I typed it by hand, but you should get the idea. Hope that helps.
我是手写的,但你应该明白了。希望有帮助。
回答by Trevor Siemens
Your class needs to be inside of your interface. This needs to be in a file called IIGetResult.java
你的类需要在你的界面内。这需要在一个名为 IIGetResult.java 的文件中
interface IIGetResult {
String getResult();
public class IgetResult implements IIGetResult{
@Override
String getResult() { return null; }
}
}
回答by ratiaris
I can see the following issues:
我可以看到以下问题:
You are missing the 'package <a/b/c>' declaration in your classes.
Your a.IIGetResult interface should be public, otherwise it won't be visible in the 'b' package.
The Java convention is for class name to start with an upper case, thus your inner class insided PracticeClass should be named 'Inner' instead.
Your inner class should have a public constructor, so that the later can be invoked from a class extending PracticeClass defined in another package.
The overriden inner.getResult() method should be public (but out-of-topic).
Your class IGetResult should be defined in a third package (c?) and should extends PracticeClass (though I must admit your instructions are a little bit confusing to me).
您的课程中缺少“package <a/b/c>”声明。
你的 a.IIGetResult 接口应该是公开的,否则它在 'b' 包中是不可见的。
Java 约定是类名以大写开头,因此您在 PracticeClass 中的内部类应该命名为“Inner”。
您的内部类应该有一个公共构造函数,以便可以从扩展在另一个包中定义的 PracticeClass 的类调用后者。
重写的 inner.getResult() 方法应该是公开的(但不在主题范围内)。
您的类 IGetResult 应该在第三个包 (c?) 中定义,并且应该扩展 PracticeClass(尽管我必须承认您的说明让我有点困惑)。
Aplly the above points along with @dimoniy's answer and you should be OK.
将以上几点与@dimoniy 的回答一起应用,你应该没问题。