英文:
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("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;
}
}
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("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;
}
}
Remember that when we call a method, we need to have a set of parenthesis at the end.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论