简体   繁体   English

无法访问默认包中的类

[英]Can't access Classes in default package

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. 基本上我的项目结构包含三个主要类,它们在一个名为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" 我得到这些错误“这个类必须实现继承的抽象方法Simulation.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". 然后,我必须创建三个类导入包“simulation”并扩展其类“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. 这些类突然进入默认包,现在他们给了我这些错误。

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. 因此,您必须创建一个具有不同名称的新包,并添加您的类或将它们放在与您的类相同的包中,并执行所需的导入。

How are you creating your classes? 你是如何创建课程的? When you create a new class, Eclipse shows a New Java Class dialog. 创建新类时,Eclipse将显示“ New Java Class对话框。 This dialog lets you choose which package to create your new class in: 此对话框允许您选择在以下位置创建新类的包:

在此输入图像描述

Just click the Browse button and you'll be able to pick a package. 只需单击“ Browse按钮,您就可以选择一个包。

在这里输入图像描述我不喜欢图像描述> :(

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. 这是因为Simulation.stop()方法的签名提到了一些对simulation包具有包限制(非公共)的类。

You solve it by either 你可以解决它

  1. Move SSQSim to the simulation package so it can access the same classes as Simulation , or SSQSim移动到simulation包,以便它可以访问与Simulation相同的类,或
  2. Make the classes required to extend the Simulation class package-public. 制作扩展Simulation类包 - 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!) (附注:根据经验,不要使用默认包。但是,即使您避开默认包,也会遇到问题!)

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类对话框,其中包含包字段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM