【问题标题】:Adding object into object array [duplicate]将对象添加到对象数组中[重复]
【发布时间】:2013-01-03 21:53:12
【问题描述】:

我正在构建一个模拟潜水比赛的程序。我有一个类Athlete,它本质上是在尝试访问另一个类中的数组元素。有人知道我如何访问这些数据吗?

这是Athlete 类:

public class Athlete {

    public static String[] name = { "Art Class", "Dan Druff", "Jen Tull" };
    public static String[] country = { "Canada", "Germany", "USA" };
    Performance[] performance = new Performance[2];

    public Athlete(String[] name, String[] country) {
        this.name = name;
        this.country = country;
        // this.event = event;
    }

    public void perform() {
        Dive mydive = new Dive(Dive.diveName, Dive.difficulty);
        Performance event = new Performance(event.dive, Performance.judgeScores);
        performance[0] = event(event.dive.diveNames[0], event.judgeScores); // here
        performance[1] = event; // Here
        performance[2] = event; // Here
    }

    public void printResults() {
    }
}

用“//Here”表示的地方我正在尝试访问下面显示的类中的数据,我想知道我该怎么做?

性能:

public class Performance {

    Dive dive = new Dive(Dive.diveName, Dive.difficulty);
    public static float[] judgeScores = new float[7];

    public Performance(Dive dive, float[] judgeScores) {
        this.dive = dive;
        // this.dive=difficulty;
        this.judgeScores = judgeScores;
        // this.judgeScores = judgeScores;
    }
}

潜水:

import java.util.Random;

public class Dive {
    public static String diveName;
    public static int difficulty;

    public static final String[] diveNames = { "reverse pike", "forward pike",
            "reverse armstand with double somersault", "reverse triple twist",
            "double forward with triple somersault", "cannon ball" };
    public static final int[] diveDifficulties = { 3, 2, 2, 4, 4, 1 };

    public static Dive chosenRandomly() {
        Random rand = new Random();
        int num = rand.nextInt(6) + 1;
        String diveName = diveNames[num];
        int difficulty = diveDifficulties[num];

        Dive dive = new Dive(diveName, difficulty);
        return dive;
    }

    public Dive(String diveName, int difficulty) {
        this.diveName = diveName;
        this.difficulty = difficulty;
    }
}

我的构造函数都错了吗?

PS:这是作业,所以有些人可能不认为我的方法是解决问题的合适方法,但请记住我正在关注说明。

提前感谢您的任何意见!

【问题讨论】:

  • 如果您使用的是可以自动格式化代码的IDE,建议您重新发布格式化代码以增强问题的可读性。

标签: java oop object


【解决方案1】:

通常这不是从其他类(甚至是公共)访问字段的好主意。您应该为您的数组字段创建getters 并访问它们。

【讨论】:

    【解决方案2】:

    假设您正在一个名为 Javadir 的目录中工作,并且您创建了四个文件,其内容如下所示。

    文件 1

    package ListPkg;
    public class List { ... }
    class ListNode {...}
    

    文件 2

    package ListPkg;
    public class NoNextItemException { ... }
    

    文件 3

    public class Test { ... }
    class Utils { ... }
    

    文件 4

    class Test2 { ... }
    

    以下是您必须使用的目录和文件名:

    文件 1 必须位于名为 ListPkg 的子目录中,位于名为 List.java 的文件中。

    文件 2 也必须在 ListPkg 子目录中,在名为 NoNextItemException.java 的文件中。

    文件 3 必须位于名为 Test.java 的文件中(在 Javadir 目录中)。

    文件 4 可以在任何 .java 文件中(在 Javadir 目录中)。

    有关更多指南,请浏览此notes on java packages

    【讨论】:

    • 它们在同一个文件夹中
    • @theolc 按照上面的例子解决你的问题
    猜你喜欢
    • 2014-03-17
    • 2022-01-05
    • 1970-01-01
    • 2022-01-13
    • 2018-09-06
    • 2014-04-21
    • 2014-06-04
    • 1970-01-01
    相关资源
    最近更新 更多