-1

我想编译一个包含两个类的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;
    }           
}
4

2 回答 2

1

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

}

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

于 2020-09-17T23:36:42.787 回答
1
class Hello_test{}

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

于 2020-09-17T22:34:22.610 回答