I have a problem with the default package. Basically my project structure consists of three main classes which extend one abstract class in a package called simulation. Yesterday I did a name change in the project and now my three main classes are put automatically in the default package, so they can't be visible by the class inside the simulation package and vice versa. For the following code
我对默认包有问题。基本上,我的项目结构由三个主要类组成,它们在一个名为模拟的包中扩展了一个抽象类。昨天我在项目中更改了名称,现在我的三个主要类被自动放到默认包中,所以它们不能被模拟包中的类看到,反之亦然。对于下面的代码
import simulation.*;
class SSQSim extends Simulation{
}
I get these errors "This class must implement the inherited abstract method Simulation.stop(), but cannot override it since it is not visible from SSQSim. Either make the type abstract or make the inherited method visible" "The type Simulation is not visible"
我得到了这些错误“这个类必须实现继承的抽象方法simul. stop(),但是不能重写它,因为它在SSQSim中是不可见的。要么使类型抽象,要么使继承的方法可见“类型模拟不可见”
Thank you in advance.
提前谢谢你。
EDIT The problem is that I don't have to use any other packages. It's basically homework and the rules for the submission are pretty strict: First, I have to submit the package "simulation" which contains the abstract class "Simulation" with some methods to implement with the help of the other classes. This part is fine.
编辑的问题是我不需要使用任何其他的包。它基本上是家庭作业,提交的规则非常严格:首先,我必须提交包“模拟”,其中包含抽象类“模拟”,并在其他类的帮助下使用一些方法实现。这部分很好。
Then, I have to create three classes which import the package "simulation" and extend its class "Simulation". They have specifically said not to put these classes in any package. At first, they all worked fine but after I renamed the project. These classes suddenly moved into the default package and now they give me these errors.
然后,我必须创建三个类,它们导入包“模拟”并扩展其类“模拟”。他们明确表示不把这些类放在任何包中。起初,他们都做得很好,但在我重新命名项目之后。这些类突然转移到默认包中,现在它们给了我这些错误。
4 个解决方案
#1
1
How are you creating your classes? When you create a new class, Eclipse shows a New Java Class
dialog. This dialog lets you choose which package to create your new class in:
如何创建类?当您创建一个新类时,Eclipse将显示一个新的Java类对话框。此对话框允许您选择在以下内容中创建新类的包:
Just click the Browse
button and you'll be able to pick a package.
只需单击Browse按钮,您就可以选择一个包。
#2
1
Why you use the default package,From the java specs:
为什么使用默认包,从java规范:
It is a compile time error to import a type from the unnamed package.
So you have to create a new package with a different name and add your classes or put them in the same package as your class and do the required imports.
因此,您必须创建一个具有不同名称的新包,并添加您的类或将它们放入与类相同的包中,并执行所需的导入。
#3
0
This is due to the fact that the signature of Simulation.stop()
method mentions some class that is package-restricted (non-public) to the simulation
package.
这是因为simul. stop()方法的签名提到了模拟包的一些类,这些类是受包限制的(非公共的)。
You solve it by either
你也可以解它。
- Move
SSQSim
to thesimulation
package so it can access the same classes asSimulation
, or - 将SSQSim移到模拟包中,这样它就可以访问与模拟相同的类,或者
- Make the classes required to extend the
Simulation
class package-public. - 使扩展模拟类包所需的类成为公共的。
To move a class from one package to another in eclipse, you can simply drag the source-code file into the desired package, and eclipse will refactor the code accordingly.
要在eclipse中将类从一个包移动到另一个包,只需将源代码文件拖放到所需的包中,eclipse将相应地重构代码。
(A side-note: As a rule of thumb, don't use the default package. The problem you ran into however, can occur even if you avoid the default package!)
(附带说明:根据经验,不要使用默认的包。然而,即使您避免了默认包,您遇到的问题也可能发生!
#4
0
This is just a hint for now, as the source code for all the classes is not posted. The error message implies the presence of an abstract method in the parent class, that cannot be overriden due to loss in visibility in the child class. Usually, this is due to the use of the default access specifier ("friendly"), or the private access specifier (which is meaningless, especially if the method is declared as abstract). One will have to use the protected access modifier on the parent, to ensure that the method is now visible to all child classes irrespective of whether they are present in the same package or not.
这只是一个提示,因为所有类的源代码都没有发布。错误消息暗示父类中存在一个抽象方法,由于子类中可见性的丢失,这个方法不能被覆盖。通常,这是由于使用默认的访问说明符(“友好”)或私有访问说明符(这是没有意义的,特别是如果方法被声明为抽象的)。一个人必须使用受保护的访问修饰符对父类,以确保该方法现在对所有子类都可见,不管它们是否存在于同一个包中。
In short, if the child cannot "see" the method that is being overriden, then the compiler issues a warning that informs about the inevitability to deduce whether the method defined in the parent or the child should used at runtime.
简而言之,如果子代不能“看到”被重写的方法,那么编译器就会发出警告,告知推断在父类或子类中定义的方法在运行时是否应该使用的必然性。
As far as Eclipse's behavior of creating classes in the default package is concerned, one can always ensure that a package is specified when creating a new class in the appropriate dialog (the new Java Class dialog, where a package field is present).
就Eclipse在默认包中创建类的行为而言,可以始终确保在适当的对话框(出现包字段的新Java类对话框)中创建新类时指定了包。
#1
1
How are you creating your classes? When you create a new class, Eclipse shows a New Java Class
dialog. This dialog lets you choose which package to create your new class in:
如何创建类?当您创建一个新类时,Eclipse将显示一个新的Java类对话框。此对话框允许您选择在以下内容中创建新类的包:
Just click the Browse
button and you'll be able to pick a package.
只需单击Browse按钮,您就可以选择一个包。
#2
1
Why you use the default package,From the java specs:
为什么使用默认包,从java规范:
It is a compile time error to import a type from the unnamed package.
So you have to create a new package with a different name and add your classes or put them in the same package as your class and do the required imports.
因此,您必须创建一个具有不同名称的新包,并添加您的类或将它们放入与类相同的包中,并执行所需的导入。
#3
0
This is due to the fact that the signature of Simulation.stop()
method mentions some class that is package-restricted (non-public) to the simulation
package.
这是因为simul. stop()方法的签名提到了模拟包的一些类,这些类是受包限制的(非公共的)。
You solve it by either
你也可以解它。
- Move
SSQSim
to thesimulation
package so it can access the same classes asSimulation
, or - 将SSQSim移到模拟包中,这样它就可以访问与模拟相同的类,或者
- Make the classes required to extend the
Simulation
class package-public. - 使扩展模拟类包所需的类成为公共的。
To move a class from one package to another in eclipse, you can simply drag the source-code file into the desired package, and eclipse will refactor the code accordingly.
要在eclipse中将类从一个包移动到另一个包,只需将源代码文件拖放到所需的包中,eclipse将相应地重构代码。
(A side-note: As a rule of thumb, don't use the default package. The problem you ran into however, can occur even if you avoid the default package!)
(附带说明:根据经验,不要使用默认的包。然而,即使您避免了默认包,您遇到的问题也可能发生!
#4
0
This is just a hint for now, as the source code for all the classes is not posted. The error message implies the presence of an abstract method in the parent class, that cannot be overriden due to loss in visibility in the child class. Usually, this is due to the use of the default access specifier ("friendly"), or the private access specifier (which is meaningless, especially if the method is declared as abstract). One will have to use the protected access modifier on the parent, to ensure that the method is now visible to all child classes irrespective of whether they are present in the same package or not.
这只是一个提示,因为所有类的源代码都没有发布。错误消息暗示父类中存在一个抽象方法,由于子类中可见性的丢失,这个方法不能被覆盖。通常,这是由于使用默认的访问说明符(“友好”)或私有访问说明符(这是没有意义的,特别是如果方法被声明为抽象的)。一个人必须使用受保护的访问修饰符对父类,以确保该方法现在对所有子类都可见,不管它们是否存在于同一个包中。
In short, if the child cannot "see" the method that is being overriden, then the compiler issues a warning that informs about the inevitability to deduce whether the method defined in the parent or the child should used at runtime.
简而言之,如果子代不能“看到”被重写的方法,那么编译器就会发出警告,告知推断在父类或子类中定义的方法在运行时是否应该使用的必然性。
As far as Eclipse's behavior of creating classes in the default package is concerned, one can always ensure that a package is specified when creating a new class in the appropriate dialog (the new Java Class dialog, where a package field is present).
就Eclipse在默认包中创建类的行为而言,可以始终确保在适当的对话框(出现包字段的新Java类对话框)中创建新类时指定了包。