导入不同目录中的类 [英] import class in different directory
问题描述
实际上,我试图在Think in Java中完成这种练习以实现自学目的 -
Actually, I am trying to finish this practice in "Think in Java" for self-learning purpose --
练习6:(2)创建一个界面至少有一种方法,在自己的包装中。在单独的包中创建
类。添加一个实现该接口的受保护内部类。在
第三个包中,继承自您的类,并在方法内返回
protected内部类的对象,在返回期间向上转换为接口。
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.
所以我在目录a下创建了一个名为IgetResult.java的类,它有一个IIGetResult接口。
so I created a class named IgetResult.java under directory "a" which has a IIGetResult Interface.
interface IIGetResult {
String getResult();
}
public class IgetResult {
}
然后我创建另一个目录中的另一个类 - 目录b
then I create another class in another directory -- directory 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");
}
}
在最后一步,我编译带命令的两个java类:
In the final step, I compile the two java classes with command:
#javac a / .java b / .java
# javac a/.java b/.java
并收到以下错误:
./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.
推荐答案
根据报价:
在自己的包中创建一个至少包含一个方法的接口。
Create an interface with at least one method, in its own package.
所以我们创建 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
所以现在我们在第三个单独的包中创建一个 InnerTest
类的子类:
So now we create a sub-class of InnerTest
class in third separate package:
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.
这篇关于导入不同目录中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!