gpt4 book ai didi

java - Java 中的编译接口(interface)

转载 作者:行者123 更新时间:2023-12-01 15:42:57 26 4
gpt4 key购买 nike

我刚刚发现了这一点,我希望有人能知道发生了什么。要遵循的命令(在临时目录中运行此命令,事先清除所有内容):

没问题:

cat << EOF > Myface.java
public interface Myface {}
EOF

cat << EOF > Myclass.java
public class Myclass implements Myface {}
EOF

javac Myface.java
javac Myclass.java

Myclass 无法编译:

cat << EOF > Myface.java
package Mypackage;
public interface Myface {}
EOF

cat << EOF > Myclass.java
package Mypackage;
public class Myclass implements Myface {}
EOF

javac Myface.java
javac Myclass.java

Myclass 无法编译:

cat << EOF > Myface.java
package Mypackage;
public interface Myface {}
EOF

cat << EOF > Myclass.java
package Mypackage;
public class Myclass implements Myface {}
EOF

javac Myclass.java

这是怎么回事?

编辑:所有命令,运行:

javatest@l-bel-smai-ubuntu:~$ ls
javatest@l-bel-smai-ubuntu:~$ pwd
/home/javatest
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myface.java
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
javatest@l-bel-smai-ubuntu:~$ ls
Myclass.class Myclass.java Myface.class Myface.java
javatest@l-bel-smai-ubuntu:~$ rm *
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> package Mypackage;
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> package Mypackage;
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myface.java
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
Myclass.java:2: cannot find symbol
symbol: class Myface
public class Myclass implements Myface {}
^
1 error
javatest@l-bel-smai-ubuntu:~$ rm *
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myface.java
> package Mypackage;
> public interface Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ cat << EOF > Myclass.java
> package Mypackage;
> public class Myclass implements Myface {}
> EOF
javatest@l-bel-smai-ubuntu:~$
javatest@l-bel-smai-ubuntu:~$ javac Myclass.java
Myclass.java:2: cannot find symbol
symbol: class Myface
public class Myclass implements Myface {}
^
1 error
javatest@l-bel-smai-ubuntu:~$

最佳答案

是的,我们开始吧:Java/Class 文件目录结构必须与声明 Java 类的包结构相匹配,编译和类加载才能正常工作。

关于java - Java 中的编译接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com