文件名和类名在java中不同 [英] File name and class name different in java

查看:145
本文介绍了文件名和类名在java中不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为In.java的文件,输入以下代码:

  class In {
int a;

public static void main(
String args []){
继承h = new Inheritance(); $ b $ h h.set(6);
System.out.println(h.get());
}

void set(
int a){
this.a = a;
}

int get(){
System.out.println(a);
return a;
}
}



当我编译它时显示了一个关于继承的错误。然后我重命名为In as I Inheritance

  class Inheritance {
int a;

public static void main(
String args []){
继承h = new Inheritance();
h.set(6);
System.out.println(h.get());
}

void set(
int a){
this.a = a;
}

int get(){
System.out.println(a);
return a;
}
}



现在当我编译它编译并创建Inheritance.class但文件名是In.java,当我编译为 public class Inheritance 它提醒我该类应该更改为Inheritance.java。现在当我运行 java在它显示一个错误为错误:无法找到或加载主类在将类重命名为In as

  class In {
int a;

public static void main(
String args []){
继承h = new Inheritance();
h.set(6);
System.out.println(h.get());
}

void set(
int a){
this.a = a;
}

int get(){
System.out.println(a);
return a;
}
}



现在当我编译时编译为In.class当我运行输出它运行程序显示



6
6



该程序与In.java并运行类的名称为类继承它编译并给出了Inheritance.class。
1.如果类名和文件名不同,编译器会出现错误吗?
当我运行 java In 它显示错误:无法找到或加载主类在因为生成了In.class文件为什么在编译In.java时没有检测到类名为Inheritance的类?所以一个类文件可以在同一目录中使用任何其他类文件?

解决方案

> public 应保存在同名的文件中。如果公用类的名称和包含它的文件不同,那么这是一个编译错误。未声明为 public 的类可以保存在不同名称的文件中。



请注意,生成的类文件是以java类命名的,而不是文件名。下面的例子中, X 是任何有效的名称(除外) Foo Bar )。



示例1:

  // filename X.java(不编译)
public class Foo {

}
public class Bar {

}

编译器抱怨公共类 Foo Bar 不在自己的 .java 文件中。 / p>

示例2:

  // filename Foo.java t compile)
public class Foo {

}
public class Bar {

}
pre>

错误与上述相同,但此次只适用于 Bar



示例3:

  // filename Foo.java(compiles)
public class Foo {

}
class Bar {

}


$ b b

生成的文件是 Foo.class Bar.class



示例4:

  // filename X.java(compiles)
class Foo {

}
class Bar {

}

生成的文件是 Foo.class Bar.class


I created a file named In.java an entered the following code

class In {
   int a;

   public static void main(
      String args[] ) {
      Inheritance h = new Inheritance();
      h.set( 6 );
      System.out.println( h.get() );
   }

   void set(
      int a ) {
      this.a = a;
   }

   int get() {
      System.out.println( a );
      return a;
   }
}

When I compiled it showed me an error regarding Inheritance. Then I renamed In as I Inheritance

class Inheritance {
   int a;

   public static void main(
      String args[] ) {
      Inheritance h = new Inheritance();
      h.set( 6 );
      System.out.println( h.get() );
   }

   void set(
      int a ) {
      this.a = a;
   }

   int get() {
      System.out.println( a );
      return a;
   }
}

Now when I compiled it compiled and created Inheritance.class but the file name was In.java still when I compile as public class Inheritance it alerts me that the class should be changed as Inheritance.java . now when I run java Init shows an error as Error: Could not find or load main class In Now I again renamed the class as In as

class In {
   int a;

   public static void main(
      String args[] ) {
      Inheritance h = new Inheritance();
      h.set( 6 );
      System.out.println( h.get() );
   }

   void set(
      int a ) {
      this.a = a;
   }

   int get() {
      System.out.println( a );
      return a;
   }
}

Now when I compiled it compiled as In.class and when I run the output it had run the program showing

6 6

When I created the program with In.java and run the class with name as Class Inheritance it compiled and gave the Inheritance.class . 1. If the class name and file name are different won't the compiler show up an error? 2. when I run the java In it shows Error: Could not find or load main class In as the In.class file is generated Why doesn't it detect it while compiling In.java with class name as Inheritance ? so can one class file use any other class file in the same directory?

解决方案

Any class which is declared public should be kept in a file of the same name. It is a compilation error if the names of the public class and the file which contains it are different. A class which isn't declared public can be kept in a file of a different name.

Note that the generated class files are named after the java classes, not the file names. Look at below examples.

In the below illustrations X is any valid name (except Foo and Bar).

Example 1:

// filename X.java (doesn't compile)
public class Foo {

}
public class Bar {

}

compiler complains about public classes Foo and Bar not being present in their own .java files.

Example 2:

// filename Foo.java (doesn't compile)
public class Foo {

}
public class Bar {

}

error same as above but applies only to Bar this time.

Example 3:

// filename Foo.java (compiles)
public class Foo {

}
class Bar {

}

generated files are Foo.class and Bar.class.

Example 4:

// filename X.java (compiles)
class Foo {

}
class Bar {

}

generated files are Foo.class and Bar.class.

这篇关于文件名和类名在java中不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆