This question already has answers here:
Java Object Oriented Programming Setters and Getters
                    
                    
                        Why does my ArrayList contain N copies of the last item added to the list?
                            
                                (5个答案)
                            
                    
                2年前关闭。
        

    

因为我自己学习过编程,所以我认为我应该开始创建适当的类和变量,但是我似乎还不太了解。

我已经读过我首先需要一个interface,所以我使我的样子如下:

public interface PitchInterface {
    void setOccuranceTime(float occuranceTime);
    float getOccuranceTime();
    void setPitch(float pitch);
    float getPitch();
}


其次,我制作了一个实现interface的类:

public class Pitch implements PitchInterface{

    float occuranceTime;
    float pitch;

    @Override
    public void setOccuranceTime(float occuranceTime) {
        this.occuranceTime = occuranceTime;
    }

    @Override
    public float getOccuranceTime() {
        return occuranceTime;
    }

    @Override
    public void setPitch(float pitch) {
        this.pitch = pitch;
    }

    @Override
    public float getPitch() {
        return pitch;
    }
}


现在,我需要给Pitch赋值,这就是我目前遇到的问题:

public class Foo {

    Pitch pitch = new Pitch();
    String[] pitches;
    List<Pitch> listOfPitches = new ArrayList<Pitch>();
    List<String> wordsList = new ArrayList<String>();

public List<Pitch> getListOfPitches(){
    getPitches();
    for (String pitchString: pitches) {
        makeListOfPitches(pitch, pitchString);
    }
    return listOfPitches;
}

public void makeListOfPitches(Pitch pitch, String pitchString){
    pitch.setPitch(getPitchesInfo(pitchString, 0));
    pitch.setOccuranceTime(getPitchesInfo(pitchString, 1));
    listOfPitches.add(pitch);
}

    public List<String> getWordsList(){
        //To-do make words out of Pitches and add them to list
        return wordsList;
    }



    public String[] getPitches() {
        pitches = pitchesRaw.split("\\r?\\n");
        return pitches;
    }

    private float getPitchesInfo(String pitch, int position){
        String[] frequencyAndTime = pitch.split("\\:");
        if(position == 0){
            return Float.parseFloat(frequencyAndTime[0].replace(',', '.'));
        }
        if(position == 1){
            return Float.parseFloat(frequencyAndTime[1].replace(',', '.'));
        }
        else return 0;
    }

    String pitchesRaw = "0,14:23281,61\n" +
            "0,23:53,65\n" +
            "0,37:72,53\n" +
            "0,56:86,09\n" +
            "0,60:88,58\n" +
            "0,65:87,45\n" +
            "0,70:87,11\n" +
            "0,74:89,56\n" +
            "0,79:96,22\n" +
            "0,84:23288,24\n" +
            "0,88:103,92\n" +
            "0,93:107,46\n" +
            "0,98:108,02\n" +
            "1,02:107,51\n" +
            "1,07:104,92\n" +
            "1,11:105,94\n" +
            "1,16:106,40\n";
    }


最后从Main类初始化它:

public static void main(String[] args) {
    Foo listOfWordsAndPithes = new Foo();
    List<Pitch> list = listOfWordsAndPithes.getListOfPitches();
    for (Pitch pitch: list) {
        System.out.println(pitch.getPitch());
    }
}



首先,如果如上所述循环输出所有pitches,则将得到以下所示的值作为输出。但是我想要的是每个音高都具有不同的值,它是从pitchesRaw

106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
106.4
其次,如果我需要通过将多个words list添加到一个列表中来制作一个Pitches,例如将6个不同的Pitchs添加到一个列表中,然后将这些Pitches添加到另一个列表中,该列表将是一个新列表。然后,我是否需要制作另一个名为classWord,并在那里定义什么单词以及如何设置/获取它?
第三,我从评论中读到,这里不需要使用Interface。我什么时候知道使用接口是否可以,什么时候不知道?

最佳答案

为了回答问题的最后一部分,在这里使用接口既不是好事,也不是坏事,实际上是在为您的吸气剂和设置器创建一个接口,是否必要取决于您要如何使用该接口。看来您的情况没有必要。看看Java Interface Usage Guidelines -- Are getters and setters in an interface bad?

关于第二个问题,您可能还不需要一个单独的类来实例化音调列表,word确实具有含义并且可以在其他地方实例化为对象吗?如果您只想要一个列表,只需执行在List<Pitch> word = new ArrayList<Pitch>();上方的操作,然后将音高添加到列表即可。

关于java - Java面向对象编程的多个类实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49386027/

10-10 17:26