7

我正在尝试在我的运动员班级、国家和姓名的两个数组上打印第一个元素。我还需要创建一个对象来模拟运动员的三次潜水尝试(最初设置为零)。我是 OOP 的新手,我不知道如何在我的主程序中执行此操作……就构造函数而言。这是我到目前为止所做的......

这是主要的:

import java.util.Random;
import java.util.List;


public class Assignment1 {
public static void main(String[] args) {
            Athlete art = new Athlete(name[0], country[0], performance[0]);   
    }
}

我真的不知道该怎么办......

这是带有数组的类。

 import java.util.Random;
 import java.util.List;

 public class Athlete {

public String[] name = {"Art", "Dan", "Jen"};
public String[] country = {"Canada", "Germant", "USA"};
    //Here i would like to create something that would be representing 3 dive attemps  (that relate to dive and score. eventually.)

 Athlete(String[] name, String[] country, Performance[] performance) {
    this.name = name;
    this.country=country;
    this.performance=performance;

}



public Performance Perform(Dive dive){
    dive.getDiveName();
    return null;        
}

public String[] getName() {
    return name;
}

public void setName(String[] name) {
    this.name = name;
}

public String[] getCountry() {
    return country;
}

public void setCountry(String[] country) {
    this.country = country;
}

    }

在此先感谢您的帮助和意见!顺便说一句,还有其他课程,只是不相关的atm ..

4

6 回答 6

5

首先,对于您的 Athlete 类,您可以删除您的Getter and Setter方法,因为您已经使用访问修饰符声明了实例变量public。您可以通过 访问变量<ClassName>.<variableName>

但是,如果您真的想使用 that Getter and Setter,请将public修饰符更改为private

其次,对于构造函数,您正在尝试执行一种简单的技术,称为shadowing. Shadowing是当您有一个方法具有与声明的变量同名的参数时。这是一个示例shadowing
----------Shadowing sample----------
您有以下课程:

public String name;

public Person(String name){
    this.name = name; // This is Shadowing
}

例如,在您的 main 方法中,您将类实例化Person如下:
Person person = new Person("theolc");

变量name将等于"theolc"
----------End of shadowing----------

让我们回到您的问题,如果您只想使用当前代码打印第一个元素,您可以删除Getter and Setter. 删除您的参数constructor

public class Athlete {

public String[] name = {"Art", "Dan", "Jen"};
public String[] country = {"Canada", "Germany", "USA"};

public Athlete() {

}

在您的主要方法中,您可以这样做。

public static void main(String[] args) {
       Athlete art = new Athlete();   

       System.out.println(art.name[0]);
       System.out.println(art.country[0]);
    }
}
于 2013-01-20T01:56:14.523 回答
4

目前您无法访问名为namecountry的数组,因为它们是Athelete类的成员变量。

根据您正在尝试执行的操作,这将不起作用。

这些数组属于您的主类。

于 2013-01-20T01:45:58.970 回答
4

您在运动员课上的尝试似乎是在与一群运动员打交道,这是设计上的错误。

定义一个类来表示单个运动员,其字段表示运动员的属性:

public class Athlete {
    private final String name;
    private final String country;
    private List<Performance> performances = new ArrayList<Performance>();
    // other fields as required

    public Athlete (String name, String country) {
        this.name = name;
        this.country = country;
    }
    // getters omitted

    public List<Performance> getPerformances() {
        return performances;
    }

    public Performance perform(Dive dive) {
        // not sure what your intention is here, but something like this:
        Performance p = new Performance(dive, this);
        // add new performance to list
        performances.add(p);
        return p;
    }
}

然后您的主要方法将像这样使用 ti:

public class Assignment1 {
    public static void main(String[] args) {
        String[] name = {"Art", "Dan", "Jen"};
        String[] country = {"Canada", "Germant", "USA"};
        Dive[] dive = new Dive[]{new Dive("somersault"), new Dive("foo"), new Dive("bar")};
        for (int i = 0; i < name.length; i++) {
            Athlete athlete = new Athlete(name[i], country[i]);
            Performance performance = athlete.perform(dive[i]);   
            // do something with athlete and/or performance
        }
    }
}
于 2013-01-20T01:55:00.093 回答
1

我认为你对你所做的事情有点搞砸了。运动员是一个对象,运动员有一个名字,我有一个他居住的城市。运动员可以潜水。

public class Athlete {

private String name;
private String city;

public Athlete (String name, String city){
this.name = name;
this.city = city;
}
--create method dive, (i am not sure what exactly i has to do)
public void dive (){} 
}




public class Main{
public static void main (String [] args){

String name = in.next(); //enter name from keyboad
String city = in.next(); //enter city form keybord

--create a new object athlete and pass paramenters name and city into the object
Athlete a = new Athlete (name, city);

}
}
于 2013-01-20T01:51:52.983 回答
0

公共静态无效主要(字符串[]参数){

        public String[] name = {"Art", "Dan", "Jen"};
        public String[] country = {"Canada", "Germant", "USA"};
        // initialize your performance array here too.

        //Your constructor takes arrays as an argument so you need to be sure to pass in the arrays and not just objects.
        Athlete art = new Athlete(name, country, performance);   

}
于 2013-01-20T01:52:40.823 回答
0

首先,数组毫无意义,让我们摆脱它们:它们所做的只是为模拟数据提供值。如何构建模拟对象一直存在争议,但很明显,创建假运动员的代码应该在单元测试中。我会为 Athlete 类使用 Joshua Bloch 的静态构建器,但是您现在只有两个属性,所以只需将它们传递给构造函数。看起来像这样:

class Athlete {

    private String name;
    private String country;

    private List<Dive> dives;

    public Athlete(String name, String country){
       this.name = name;
       this.country = country;
    }

    public String getName(){
        return this.name;
    }

    public String getCountry(){
        return this.country;
    }

    public String getDives(){
        return this.dives;
    }

    public void addDive(Dive dive){
        this.dives.add(dive);
    }
}

然后是潜水课:

class Dive {

    private Athlete athlete;
    private Date date;
    private double score;

    public Dive(Athlete athlete, double score){
        this.athlete = athlete;
        this.score = score;
        this.date = new Date();
    }

    public Athlete getAthlete(){
        return this.athlete;
    }

    public Athlete getAthlete(){
        return this.athlete;
    }

    public Athlete getAthlete(){
        return this.athlete;
    }

}

然后进行单元测试并构建类,并操作它们,确保它们正常工作。现在他们什么都不做,所以你所能做的就是断言他们保留了你放入其中的 Dives。例子:

@Test
public void testThatDivesRetainInformation(){
    Athlete art = new Athlete("Art", "Canada");
    Dive art1 = new Dive(art, 8.5);
    Dive art2 = new Dive(art, 8.0);
    Dive art3 = new Dive(art, 8.8);
    Dive art4 = new Dive(art, 9.2);

    assertThat(art.getDives().size(), is(5));
    }

然后你可以完成并添加测试,比如确保你不能在没有运动员的情况下进行潜水等等。

您可以将运动员的构造转移到测试的设置方法中,以便您可以在任何地方使用它。大多数 IDE 都支持通过重构来做到这一点。

于 2013-01-20T02:29:53.430 回答