【问题标题】:How to compile several classes in one ,java file [duplicate]如何在一个java文件中编译多个类[重复]
【发布时间】:2020-09-17 22:26:56
【问题描述】:

我想编译一个包含两个类的 java 源文件。我怎样才能做到这一点?我编译我的代码:

javac -classpath Class_ex_1.java
public class Class_ex_1 { 
    public static void main(String[] args) {        
        int del = 7;
        int del_1 = 2;
        int rem = del % del_1;
        System.out.println("First value :" + del + "\n");
        System.out.println("Second value :" + del_1 + "\n"); // 
        System.out.println("Display meaning a % b :" + rem + "\n"); //              
            
        Hello_test n1 = new Hello_test();
        System.out.println("Display parameter from class:" + n1.getColor + "\n");
                        
    }
}
    
public class Hello_test {
    String color = "Red";       
    public String getColor(){
        return color;
    }           
}

【问题讨论】:

  • JLS 只允许每个文件有一个公共类,并且该类必须与文件同名 (FileName.java -> public Class FileName { ... })。因此,提供的代码不是有效的 java 代码,因此无法编译。
  • 第 1 步:从与文件命名不同的类中删除 public。 --- 第 2 步:从命令中删除 -classpath
  • 我这样做了,我得到了错误:javac Class_ex_1.java Class_ex_1.java:13: 错误:找不到符号 System.out.println("显示类中的参数:" + n1.getColor + " \n"); ^ 符号:变量 getColor 位置:Hello_test 类型的变量 n1 1 错误
  • 您错误地调用了该方法。将n1.getColor 更改为n1.getColor()
  • 这能回答你的问题吗? Can a java file have more than one class?

标签: java java-console


【解决方案1】:
class Hello_test{}

只需从第二类中删除 public。

【讨论】:

  • 我这样做了,我得到了错误:javac Class_ex_1.java Class_ex_1.java:13: 错误:找不到符号 System.out.println("显示类中的参数:" + n1.getColor + " \n"); ^ 符号:变量 getColor 位置:Hello_test 类型的变量 n1 1 错误
【解决方案2】:

以下代码无法编译,因为找不到getColor

public class Class_ex_1 { 

   public static void main(String[] args) {        
      int del = 7;
      int del_1 = 2;
      int rem = del % del_1;
      System.out.println("First value :" + del + "\n");
      System.out.println("Second value :" + del_1 + "\n"); // 
      System.out.println("Display meaning a % b :" + rem + "\n"); //              
          
      Hello_test n1 = new Hello_test();
      System.out.println("Display parameter from class:" + n1.getColor + "\n");
                      
   }
}
    
class Hello_test {
   String color = "Red";       
   public String getColor(){
      return color;
   }           
}

所以它应该如下所示:

class Class_ex_1 { 

 


   public static void main(String[] args) {        
      int del = 7;
      int del_1 = 2;
      int rem = del % del_1;
      System.out.println("First value :" + del + "\n");
      System.out.println("Second value :" + del_1 + "\n"); // 
      System.out.println("Display meaning a % b :" + rem + "\n"); //              
          
      Hello_test n1 = new Hello_test();
      System.out.println("Display parameter from class:" + n1.getColor() + "\n");
                      
   }
}
    
class Hello_test {
   String color = "Red";   
       
   public String getColor(){
      return color;
   }          
       

}

请记住,当我们调用一个方法时,我们需要在末尾有一组括号。

【讨论】:

  • 您回答的问题与问题中提出的问题不同。
  • @Scratte 这只是更冗长的答案,虽然略有变异。
  • @TheCreatingCoder 非常感谢您的全面回复!!
  • @BilowYuriy 不,它没有。它回答了您的另一个问题,但根本没有解决“如何在一个 java 文件中编译多个类”。
  • @Scratte 好的,你是对的!我为其他问题设置了正确的标记
猜你喜欢
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2021-07-25
  • 2014-03-27
相关资源
最近更新 更多