Java中的字符串数组对象

7

我想在我的运动员类中打印国家和姓名两个数组的第一个元素。我还需要创建一个对象来模拟运动员进行的三次跳水尝试(最初设置为零)。我对面向对象编程很陌生,不知道如何在我的主函数中处理这个问题...至于构造函数方面,这是我目前所做的:

这是主要代码:

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;
}

    }

感谢您提前的帮助和贡献!顺便说一下,还有其他类别,但目前不相关...

6个回答

5

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

不过,如果您真的想要使用那个Getter和Setter,请将public修饰符改为private

其次,对于构造函数,您正在尝试使用一种称为shadowing的简单技术。 Shadowing是指具有与声明变量相同名称的参数的方法。以下是一个shadowing示例:
---------- Shadowing 示例 ----------
您有以下类:

public String name;

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

在您的主方法中,例如,您可以按照以下方式实例化Person类:
Person person = new Person("theolc");
变量name将等于"theolc"
----------阴影结束----------
让我们回到您的问题,如果您只想使用当前代码打印第一个元素,则可以删除Getter和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]);
    }
}

然而,我无法理解您试图使用dive做什么。 - Michael 'Maik' Ardan

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;
    }
}

然后,您的主方法会像这样使用它:
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
        }
    }
}

4

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

从您试图实现的功能来看,这样做是行不通的。

这些数组应该放在您的主类中。


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);

}
}

0

public static void main(String[] args) {

公共静态无返回值主函数(String[] args){
        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);   

}

0
首先,这些数组是没有意义的,让我们把它们去掉:它们只是为模拟数据提供值。如何构建模拟对象已经被反复讨论过了,但显然,创建假运动员的代码应该在一个单元测试中。我会使用 Joshua Bloch 的静态构建器来创建 Athlete 类,但现在你只有两个属性,所以只需将它们传递给构造函数即可。代码看起来像这样:
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);
    }
}

然后是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;
    }

}

然后进行单元测试,构建类并操作它们,确保它们正常工作。现在它们什么也不做,所以你只能断言它们是否保留了你放入其中的潜水数据。例如:

@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都支持使用重构进行此操作。


真的吗?那么将值放入数组中,并使用for方法进行迭代,调用构造函数,使用常量,例如:new Athlete(dataset[i][name], dataset[i][country]); - Rob
OP没有提到TDD。虽然这是一个很好的话题,但它不是这个问题的答案。 - LocalPCGuy
是的,很好的观点,投票反对鼓励未提及最佳实践的答案。谢谢,.NET警察! - Rob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接