如何解决如何在一个Java文件中编译几个类
我想用两个类在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{}
只需从第二类中删除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;
}
}
请记住,当我们调用一个方法时,我们需要在最后加上一组括号。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。