I have three different classes:
我有三个不同的类:
Woman.java
package human;
public class Woman extends Human{
private final String pnr;
private final String namn;
public Woman(String n, String p){
namn = n;
pnr = p;
}
public String toString(){
return "My name is "+namn+" and I am a woman.";
}
}
Man.java
package human;
public class Man extends Human{
private final String pnr;
private final String namn;
public Man(String n, String p){
namn = n;
pnr = p;
}
public String toString(){
return "My name is "+namn+" and I am a man.";
}
}
Human.java
package human;
public abstract class Human{
public static Human create(String namn, String pnr){
char nastsist_char = pnr.charAt(9); // takes second last char in pnr
String nastsist_string = Character.toString(nastsist_char);
float siffra = Float.parseFloat(nastsist_string); //Converts to float
if ((siffra % 2) == 0){ //Checks if even
return new Man(namn, pnr);
}
else{
return new Woman(namn, pnr);
}
}
}
When I try to compile the file Human.java, I get the error
当我尝试编译Human.java文件时,我收到错误
cannot find symbol: class Man,
找不到符号:类男,
and
cannot find symbol: class Woman.
找不到符号:班女。
When I try to compile the file Man.java or Woman.java, I get the error
当我尝试编译Man.java或Woman.java文件时,我收到错误
cannot find symbol: class Human.
找不到符号:类人类。
No changing of public/private
visibility of the classes have helped. All the files are located in a directory called human
.
没有改变类的公共/私人可见性有帮助。所有文件都位于名为human的目录中。
What is the problem here?
这里有什么问题?
I should add that the goal I'm trying to achieve is to have an abstract class Human that can not be instantiated, and in that class I want to have a create method that returns instances of either Man or Woman depending on pnr
. Also, I do not want Man or Woman to be possible to instantiate in any other way than through the create method.
我应该补充一点,我想要实现的目标是拥有一个无法实例化的抽象类Human,并且在该类中我想要一个create方法,根据pnr返回Man或Woman的实例。此外,我不希望男人或女人可以通过create方法以任何其他方式实例化。
Thanks
4 个解决方案
#1
There's no problem with your code or packaging, so the problem is elsewhere. How do you compile? I'm guessing you might be doing
您的代码或包装没有问题,因此问题出在其他地方。你怎么编译?我猜你可能会这样做
javac human/Human.java
which will not work, since Man
and Woman
is then not on the classpath and thus not known to the Java compiler when working on Human
. You should include these when compiling:
这是行不通的,因为男人和女人不在类路径上,因此在处理Human时Java编译器不知道。编译时应包括以下内容:
javac human/*.java
Cheers,
#2
Do you compile them by yourself, in a terminal or something? Try to compile all files all together then:
你自己编译,在终端或其他东西?尝试一起编译所有文件然后:
javac Man.java Woman.java Human.java
#3
You have cycles in the classes. So you have two choices. 1) Compile all together 2) Create a new class e.g. Factory and instantiate the objects there
你在课堂上有周期。所以你有两个选择。 1)一起编译2)创建一个新类,例如工厂并在那里实例化对象
It's never a good idea to create cycles in you code
在代码中创建循环永远不是一个好主意
#4
Have you tried : javac *.java
in your folder ?
你试过:你的文件夹中有javac * .java吗?
Java: How can I compile an entire directory structure of code ?
Java:如何编译整个代码目录结构?
#1
There's no problem with your code or packaging, so the problem is elsewhere. How do you compile? I'm guessing you might be doing
您的代码或包装没有问题,因此问题出在其他地方。你怎么编译?我猜你可能会这样做
javac human/Human.java
which will not work, since Man
and Woman
is then not on the classpath and thus not known to the Java compiler when working on Human
. You should include these when compiling:
这是行不通的,因为男人和女人不在类路径上,因此在处理Human时Java编译器不知道。编译时应包括以下内容:
javac human/*.java
Cheers,
#2
Do you compile them by yourself, in a terminal or something? Try to compile all files all together then:
你自己编译,在终端或其他东西?尝试一起编译所有文件然后:
javac Man.java Woman.java Human.java
#3
You have cycles in the classes. So you have two choices. 1) Compile all together 2) Create a new class e.g. Factory and instantiate the objects there
你在课堂上有周期。所以你有两个选择。 1)一起编译2)创建一个新类,例如工厂并在那里实例化对象
It's never a good idea to create cycles in you code
在代码中创建循环永远不是一个好主意
#4
Have you tried : javac *.java
in your folder ?
你试过:你的文件夹中有javac * .java吗?
Java: How can I compile an entire directory structure of code ?
Java:如何编译整个代码目录结构?