如何在一个java文件中编译多个类

如何在一个java文件中编译多个类,java,java-console,Java,Java Console,我想用两个类在java源文件中编译。我该怎么做?我用以下代码编译代码: javac -classpath Class_ex_1.java 只需从第二个类中删除public即可。以下代码无法编译,因为它找不到getColor public class Class_ex_1 { public static void main(String[] args) { int del = 7; int del_1 = 2; int rem = d

我想用两个类在java源文件中编译。我该怎么做?我用以下代码编译代码:

javac -classpath Class_ex_1.java

只需从第二个类中删除public即可。

以下代码无法编译,因为它找不到
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;
   }          
       

}

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

JLS只允许每个文件有一个公共类,并且该类必须与文件同名(
FileName.java
->
公共类FileName{…}
)。因此,提供的代码不是有效的java代码,因此无法编译。步骤1:从与文件命名不相同的类中删除
public
步骤2:从命令中删除
-classpath
。我这样做了,得到了错误:javac Class_ex_1.java Class_ex_1.java:13:错误:找不到符号System.out.println(“类中的显示参数:”+n1.getColor+“\n”);^symbol:variable getColor location:Hello_test 1 error类型的变量n1您错误地调用了该方法。将
n1.getColor
更改为
n1.getColor()
这是否回答了您的问题?我这样做了,得到了错误:javac Class_ex_1.java Class_ex_1.java:13:error:找不到symbol System.out.println(“类中的显示参数:”+n1.getColor+“\n”);^符号:变量getColor位置:类型为Hello_test 1 Error的变量n1非常感谢!!您回答的问题与问题中提出的问题不同。@Scratte这只是一个更详细的答案,虽然有点变异。@CreatingCoder非常感谢您的全面回答@不,它不是。它回答了您的另一个问题,但根本没有解决“如何在一个java文件中编译多个类”。@Scratte好的,您是对的!我对另一个问题做了正确的标记
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;
   }          
       

}