简体   繁体   English

同一包内的Java继承未找到类

[英]Java Inheritance within the same package not finding class

I have two classes within the same folder and package.我在同一个文件夹和包中有两个类。 The location of them is code/src/enemy.它们的位置是 code/src/enemy。 These are the two classes:这是两个类:

package src.enemy;

public class Enemy{
  String name = "";
  float maxHealth = 100;
  float health;
  public Enemy(String name) // This made me laugh more than it should
  {
    this.name = name;
    this.health = this.maxHealth;
  }
}

and

package src.enemy;

public class baseEnemy extends Enemy{
public baseEnemy(String name)
{
   super(name);
}

} }

For some reason when I try to compile them I get:出于某种原因,当我尝试编译它们时,我得到:

:3: error: cannot find symbol
public class baseEnemy extends Enemy{
 symbol: class Enemy

I'm Compiling with Javac.我正在使用 Javac 进行编译。 I'm not really sure why baseEnemy can't inherit enemy when they are in the same folder and package.我不确定为什么 baseEnemy 在它们位于同一文件夹和包中时不能继承敌人。

It's because you have no constructor in your baseEnemy Class.这是因为您的 baseEnemy 类中没有构造函数。 If you change it to:如果将其更改为:

public class Enemy {公共类敌人{

String name = "";
float maxHealth = 100;
float health;
public Enemy(String name) // This made me laugh more than it should
{
    this.name = name;
    this.health = this.maxHealth;
}

public Enemy() {
}
}

and then接着

public class baseEnemy extends Enemy {

public baseEnemy(String name) {
    super(name);
}
}

it'll be able to feed the name.它将能够提供名称。 Ideally level 0 Classes should always have a 0 argument constructor :).理想情况下,0 级类应该总是有一个 0 参数构造函数:)。 Hope that helps.希望有帮助。

javac ~/Documents/Assignments/Ass4/Code/src/enemy/Enemy.java. 

No. You should be in ~/Documents/Assignments/Ass4/Code , and issue不。你应该在~/Documents/Assignments/Ass4/Code ,并发出

javac src/enemy/Enemy.java

and similarly for the other class.其他班级也类似。

(Posted on behalf of the question author) . (代表问题作者发表)

So initially I made the directory and "Code" had a capital c.所以最初我制作了目录,“代码”有一个大写的 c。 I Then saw this and made a new folder with a small c.我然后看到了这个,并用小c创建了一个新文件夹。 What I didn't realise was that my atom code was referencing the one in the folder with a capital C.我没有意识到我的原子代码正在引用文件夹中带有大写字母 C 的代码。

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

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