【发布时间】:2012-03-04 00:25:32
【问题描述】:
我对 JAVA 有点陌生。我在大学里一直在使用它,但我不得不承认,我的导师完全没有帮助。她自己几乎不知道 JAVA,但这本身就是另一个问题。我一直对方法和类的工作方式感到困惑。我正在创建这个程序,它使用两个文件,一个“主”文件和一个“测试”文件。我似乎无法正确获取“主”文件,因为编译器一直告诉我它找不到符号,即使它们是。在“测试”文件中,我似乎无法让编译器识别“主”文件中的方法。我已确保文件位于同一文件夹中。为了简单起见,我想将它们合并到一个文件中,但我会失分。到目前为止,我已经包含了我的代码。我不是在寻找“修复它”的解决方案,我只是想弄清楚它为什么不起作用。任何帮助表示感谢,因为我的导师没有太多帮助,谢谢!
主文件:
import java.util.Scanner;
class Fruit1 {
static Scanner console = new Scanner(System.in);
public static void main(String args[]) {
String color;
String taste;
}
public Fruit1() {
// generic constructor
color = "red";
taste = "yum";
}
public Fruit1(String aColor, String aTaste) {
// constructor with parameters
color = aColor;
taste = aTaste;
}
public Fruit1(String bColor, String bTaste) {
color = bColor;
taste = bTaste;
}
String getTaste() {
return taste;
}
String getColor() {
// Accessor method
return color;
}
}
测试文件:
import java.util.*;
public class Fruit1Test {
static Scanner console = new Scanner(System.in);
public static void main(String args[]){
Fruit1 a = new Fruit1("pinkish-red", "sweet-tart");
Fruit1 l = new Fruit1("yellow", "tart/sour");
a.taste();
a.color();
l.taste();
l.color();
System.out.println("Your apple is " + a.color + "in color and has a " + a.taste + " taste. ");
System.out.println("Your lemon is " + l.color + "in color and has a " + l.taste + " taste. ");
System.out.println();
}
}
【问题讨论】:
-
你确定你的文件名是
Fruit1.java和Fruit1Test.java?? -
请复制命令行和错误(您可以尝试使用 IDE 吗?它可以帮助您专注于学习方面。)由于您处于学习模式,因此有助于阅读有关“junit”的更多信息 -它有助于改进测试。
标签: java class methods constructor symbols