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

huangapple go评论62阅读模式
英文:

How to compile several classes in one ,java file

问题

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

I would like to compile in a java source file with two classes. How can I do that? I compile my code with:

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;
    }           
}

</details>


# 答案1
**得分**: 1

class Hello_test{}
只需从第二个类中删除public关键字。还要确保另一个类位于Class_ex_1内部。

<details>
<summary>英文:</summary>

    class Hello_test{}
Simply remove public from the second class. Also make sure the other class is inside of the Class_ex_1

</details>



# 答案2
**得分**: 1

以下是翻译好的内容:

```java
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;
    }          

}

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

英文:

The following code doesn't compile because it cannot find 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(&quot;First value :&quot; + del + &quot;\n&quot;);
      System.out.println(&quot;Second value :&quot; + del_1 + &quot;\n&quot;); // 
      System.out.println(&quot;Display meaning a % b :&quot; + rem + &quot;\n&quot;); //              
          
      Hello_test n1 = new Hello_test();
      System.out.println(&quot;Display parameter from class:&quot; + n1.getColor + &quot;\n&quot;);
                      
   }
}
    
class Hello_test {
   String color = &quot;Red&quot;;       
   public String getColor(){
      return color;
   }           
}

So it should instead, look like the following:

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(&quot;First value :&quot; + del + &quot;\n&quot;);
      System.out.println(&quot;Second value :&quot; + del_1 + &quot;\n&quot;); // 
      System.out.println(&quot;Display meaning a % b :&quot; + rem + &quot;\n&quot;); //              
          
      Hello_test n1 = new Hello_test();
      System.out.println(&quot;Display parameter from class:&quot; + n1.getColor() + &quot;\n&quot;);
                      
   }
}
    
class Hello_test {
   String color = &quot;Red&quot;;   
       
   public String getColor(){
      return color;
   }          
       

}

Remember that when we call a method, we need to have a set of parenthesis at the end.

huangapple
  • 本文由 发表于 2020年9月18日 06:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63946911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定