关于 "why only one public class per file " 主题的给定答案的问题
Questions about given answer on topic of "why only one public class per file "
我来到了以下问题:Why only 1 public class in Java file
还有这个答案:
To understand the basic reason behind these restrictions, let's
suppose compiler doesn't give compile error for not naming file name
same as public class name.
Suppose there is a package A
A
/ \
file1.java file2.java
file1.java
package A;
class file1
{
public static void main(String args[])
{
}
}
public class file3
{
public static void main(String args[])
{
}
}
Now as we know that a public class can also be accessed outside the
package, now it will become the responsibility of a developer to make
it accessible to the outside world. Let's see how:
Suppose package A is containing only Java files(no class files) and
some class outside the package A tries to access public class file3,
compiler will first try to find file3.class ( not available ), then it
will try to find file3.java ( not available ). So even though file3
class is public in nature, it is not visible to the outside world. So
if a compiler puts the restriction that if a file is containing public
class, it should be named same as the public class name, then above
issue can be resolved and the developer won't have to think of
exposing public class to the outside world.
Compiler also puts the restriction that there should be atmost one
public class per Java file, so that every public class can be accessed
by the outside world.
但我们不会使用类似 import folder.packagename.filenameofclassfile3;
的东西,这样它在技术上仍然有效吗?
如果 .class-file
在编译时不存在
我们基本上会通过使用完整限定名告诉编译器在哪里可以找到 class file3
。
这个解释或多或少是假的。 public class 文件的源文件名必须与 class 名称相同的真正原因是因为规范如此规定。就这么简单。
import 语句没有做任何特别的事情;您可以导入一个根本未使用的 class 文件,这将导致 class 文件中的字面内容为 NOTHING;如果当您 运行 代码时导入的 class 甚至不存在,那也没关系。这不是 python 或脚本语言;导入不加载它。根本。 import com.foo.Bar;
所做的只是说:"Anytime Bar shows up in this file as a type name, imagine it read com.foo.Bar instead"(因此,您可以删除 import 语句,并将所有出现的 Bar
替换为 com.foo.Bar
并且该文件仅适用于一样。
因此,将 文件名 放在导入语句中是不合法的 java.
另请注意,如果您将 non public class 放入文件中,即使该文件与 class,很好,如果你编译它,仍然会导致 class 在它自己的文件中独立存在(编译 1 java 文件可能会产生超过 1 class 文件!)
如果我们不得不猜测为什么有人在 java 规范中写道 public classes 必须在同名文件中...谁知道呢,真的。没有真正的原因。您发现的虚假答案试图驱动的是,如果您使用 javac 非常有限的能力来查找还必须编译的源文件(使用 -sourcepath
选项,我想这可能确实更容易,但请注意,没有什么可以阻止您编写引用同一包中另一个非 public、尚未编译的 class 和 运行 仅 javac ThatFile.java
的代码] 导致相同的 'now the source file that needs to be compiled is hard to find' 问题。因此,为什么你找到的答案是假的。
我来到了以下问题:Why only 1 public class in Java file
还有这个答案:
To understand the basic reason behind these restrictions, let's suppose compiler doesn't give compile error for not naming file name same as public class name.
Suppose there is a package A
A
/ \
file1.java file2.java
file1.java
package A;
class file1
{
public static void main(String args[])
{
}
}
public class file3
{
public static void main(String args[])
{
}
}
Now as we know that a public class can also be accessed outside the package, now it will become the responsibility of a developer to make it accessible to the outside world. Let's see how:
Suppose package A is containing only Java files(no class files) and some class outside the package A tries to access public class file3, compiler will first try to find file3.class ( not available ), then it will try to find file3.java ( not available ). So even though file3 class is public in nature, it is not visible to the outside world. So if a compiler puts the restriction that if a file is containing public class, it should be named same as the public class name, then above issue can be resolved and the developer won't have to think of exposing public class to the outside world.
Compiler also puts the restriction that there should be atmost one public class per Java file, so that every public class can be accessed by the outside world.
但我们不会使用类似 import folder.packagename.filenameofclassfile3;
的东西,这样它在技术上仍然有效吗?
如果 .class-file
在编译时不存在
我们基本上会通过使用完整限定名告诉编译器在哪里可以找到 class file3
。
这个解释或多或少是假的。 public class 文件的源文件名必须与 class 名称相同的真正原因是因为规范如此规定。就这么简单。
import 语句没有做任何特别的事情;您可以导入一个根本未使用的 class 文件,这将导致 class 文件中的字面内容为 NOTHING;如果当您 运行 代码时导入的 class 甚至不存在,那也没关系。这不是 python 或脚本语言;导入不加载它。根本。 import com.foo.Bar;
所做的只是说:"Anytime Bar shows up in this file as a type name, imagine it read com.foo.Bar instead"(因此,您可以删除 import 语句,并将所有出现的 Bar
替换为 com.foo.Bar
并且该文件仅适用于一样。
因此,将 文件名 放在导入语句中是不合法的 java.
另请注意,如果您将 non public class 放入文件中,即使该文件与 class,很好,如果你编译它,仍然会导致 class 在它自己的文件中独立存在(编译 1 java 文件可能会产生超过 1 class 文件!)
如果我们不得不猜测为什么有人在 java 规范中写道 public classes 必须在同名文件中...谁知道呢,真的。没有真正的原因。您发现的虚假答案试图驱动的是,如果您使用 javac 非常有限的能力来查找还必须编译的源文件(使用 -sourcepath
选项,我想这可能确实更容易,但请注意,没有什么可以阻止您编写引用同一包中另一个非 public、尚未编译的 class 和 运行 仅 javac ThatFile.java
的代码] 导致相同的 'now the source file that needs to be compiled is hard to find' 问题。因此,为什么你找到的答案是假的。