如何在一个 java 文件中编译多个 类
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;
}
}
class Hello_test{}
只需从第二个 class 中删除 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;
}
}
记住我们调用方法的时候,最后要有一组括号。
我想在一个 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;
}
}
class Hello_test{}
只需从第二个 class 中删除 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;
}
}
记住我们调用方法的时候,最后要有一组括号。