Java 面向对象编程多个 class 实例

Java Object Oriented Programming multiple class instances

自从我自学了编程,我想我应该开始创建适当的 classes 和变量,但我似乎不太明白。

我读到首先我需要一个 interface,所以我把我的看起来像这样:

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

其次,我制作了一个 class 实现了 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 初始化它 class:

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

回答你问题的最后一部分,在这里使用接口没有好坏之分,你实际上是在为你的getters和setters创建一个接口,是否有必要取决于你想如何使用接口。看起来你的情况没有必要。看看Java Interface Usage Guidelines -- Are getters and setters in an interface bad?

关于你的第二个问题,你可能也不需要单独的 class 来实例化音高列表,word 是否真的有意义并且会在其他地方实例化为对象?如果您只想要一个列表,只需按照上面 List<Pitch> word = new ArrayList<Pitch>(); 所做的操作,然后将球场添加到列表中即可。