【问题标题】:How Can I Write a Method that References the Object that is Calling it?如何编写引用正在调用它的对象的方法?
【发布时间】:2016-07-20 00:29:05
【问题描述】:

我正在尝试制作一个程序,打印出您输入的三个数字,然后输出这些数字的平均值。创建所需方法后,当我编译代码时,使用带有scores 的方法时出现错误。我不确定在调用mPrint()average() 时如何引用scores

编译代码时,会在scores.mPrint(3);scores.average(); 所在的行抛出错误。这些错误是: The method(方法在这里)is undefined for the type ArrayList<Double>.

我导入了:java.util.ArrayList 和 java.util.Scanner

public class OOPtraining {

ArrayList<Double> scores = new ArrayList<Double>();

public void mPrint(Integer prints) {

    for (Integer i =0;i<prints; i++) {
        System.out.println(scores.get(i));
    }

}

public void average() {

    double divi = scores.get(0)+scores.get(1)+scores.get(2);
    System.out.println(divi/3);

}

public void main(String[] args) {

    ArrayList<Double> scores = new ArrayList<Double>();

    Scanner reader = new Scanner(System.in);


    // down here is where I input the scores and then add them to "scores"
    System.out.println("Enter a score: ");
    double score1 = reader.nextDouble();
    while (true) {
        System.out.println("Type a double-type number:");
        try {
            score1 = Double.parseDouble(reader.next());
            break;
        } catch (NumberFormatException ignore) {
            System.out.println("Invalid input");
        }
    }
    scores.add(score1);


    System.out.println("Enter another score: ");
    double score2 = reader.nextDouble();
    while (true) {
        System.out.println("Type a double-type number:");
        try {
            score2 = Double.parseDouble(reader.next());
            break;
        } catch (NumberFormatException ignore) {
            System.out.println("Invalid input");
        }
    }
    scores.add(score2);


    System.out.println("Enter another score: ");
    double score3 = reader.nextDouble();
    while (true) {
        System.out.println("Type a double-type number:");
        try {
            score3 = Double.parseDouble(reader.next());
            break;
        } catch (NumberFormatException ignore) {
            System.out.println("Invalid input");
        }
    }
    scores.add(score3);
    //here is where I stop adding the scores to "scores"


    scores.mPrint(3);
    scores.average();


    }
}

【问题讨论】:

  • get(...) 中有什么?
  • 在哪方面看起来不正确?
  • 请告诉我们您希望这样做。这样做毫无意义。
  • 你的问题很不清楚,请更明确一点
  • 对不起,上面的问题很神秘,但我想知道的是如何引用 ArrayList,如果它被调用(在这种情况下,scores)当我创建一个方法时。

标签: java oop arraylist methods this


【解决方案1】:
this.get(i) 

这将导致编译失败,因为您声明的类没有 get 方法。不过,目前还不清楚您到底想做什么。

我知道你现在正在尝试做什么,你正在尝试访问你的类的成员变量,“this”指的是你在 OOPTraining 中的类。您要做的是访问分数 ArrayList。为了能够做到这一点,分数必须是类变量,而不是主方法中的局部变量。看看那个,这应该让你开始。

【讨论】:

  • 我最近添加了其余代码以进行澄清。我不认为回答者需要其余部分,但希望这会有所帮助。
【解决方案2】:

在您尝试调用 this.get(int) 的同一类中必须有一些名为 get() 的方法 否则它将无法正常工作。 我认为您正在使用列表,忘记扩展它

【讨论】:

  • 我最近在 Codecademy 上练习了方法、ArrayLists 和 HashMaps,但我不完全确定如何使用“this”,因为我没有完全接触过它。
【解决方案3】:

this 指的是您的类OOPtraining 的某个实例。 this.get(0) 在您的类OOPtraining 上调用get() 方法,并将0 作为该方法的第一个也是唯一的参数传递。

问题是您的班级OOPtraining 没有 get() 方法。您要么需要将 get() 方法添加到 OOPtraining,要么更可能的是,找出您希望在什么上调用 get() 并提供该类的实例。然后代码看起来像myList.get(0)

【讨论】:

    【解决方案4】:

    如果 this.get(i) 失败,那么您可能犯了 2 个错误。

    首先,您的类可能没有名称为get 的方法。因此,要纠正错误,请在您的类中定义 get 方法。

    其次,如果您确实有 get 方法,您可能已经使用 static 关键字定义了它。在这种情况下,从方法的定义中删除 static 关键字。另一种解决方案是将this 关键字替换为您的类名。静态方法独立于对象,因此可以在不构造对象的情况下调用。

    【讨论】:

    • static 如何影响我的代码,这取决于我是否使用它?
    • 我刚刚编辑了我的答案,你可以搜索更多。
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多