【问题标题】:String Array object in JavaJava中的字符串数组对象
【发布时间】:2013-01-03 11:00:50
【问题描述】:

我正在尝试在我的 Athlete 类、国家和名称的两个数组上打印第一个元素。我还需要创建一个对象来模拟运动员的三次潜水尝试(最初设置为零)。我是 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..

【问题讨论】:

    标签: java arrays oop


    【解决方案1】:

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

    但是,如果您真的想使用 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() {
    
    }
    

    在你的 main 方法中,你可以这样做。

    public static void main(String[] args) {
           Athlete art = new Athlete();   
    
           System.out.println(art.name[0]);
           System.out.println(art.country[0]);
        }
    }
    

    【讨论】:

    • 但是,我不明白你想用潜水做什么。
    【解决方案2】:

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

    根据您正在尝试执行的操作,这是行不通的。

    这些数组属于你的主类。

    【讨论】:

      【解决方案3】:

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

      定义一个代表单个运动员的类​​,其中包含代表运动员属性的字段:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          public static void main(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);   
          
          }
          

          【讨论】:

            【解决方案6】:

            首先,数组毫无意义,让我们摆脱它们:它们所做的只是为模拟数据提供值。如何构建模拟对象一直存在令人作呕的争论,但很明显,创建假运动员的代码应该在单元测试中。我会为 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 都支持通过重构来做到这一点。

            【讨论】:

            • 认真的吗?然后将值放入数组并使用 for 方法进行迭代,调用构造函数,使用常量,例如:new Athlete(dataset[i][name], dataset[i][country]);
            • OP 中没有提到 TDD。虽然这是一次很棒的对话,但这不是这个问题的答案。
            • 是的,好点,否决鼓励未提及的最佳实践的答案。谢谢,.NET 警察!
            猜你喜欢
            • 2012-10-23
            • 1970-01-01
            • 1970-01-01
            • 2015-07-26
            • 2022-12-02
            • 1970-01-01
            • 2020-02-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多