面向Java面向对象的编程设置器和读取器

时间:2018-03-20 12:06:55

标签: java class object interface

由于我自己研究了编程,我认为我应该开始创建适当的类和变量,但我似乎没有得到它。

我已经读过,首先我需要和界面,所以我让我看起来像这样:

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

其次,我创建了一个implements 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";
    }

最后从一些主要类中初始化它:

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

我不确定我是否要求太多,但我想了解classes如何在Java中正常工作,以及我是否理解了类和接口的正确方法。另外,如何在其上制作新的Pitchesget themiterate

编辑:按照评论中的建议编辑了Foo类,现在没有错误。还有两件我似乎无法理解的事情 First如果我打印出我应该拥有的所有音高,它只会给我添加最后一个字符串的音高,但它们都应该不同。
Secondly如果我需要通过将多个words list添加到一个列表中(例如将6个不同的Pitch添加到1个列表中)来提出Pitches,然后添加Pitches进入另一个列表,该列表将成为新列表的第一个元素。那么我是否需要制作另一个名为class的{​​{1}},并在那里定义哪个词以及如何设置/获取它? Word我从评论中读到,不需要在这里使用Interface。我什么时候知道使用接口是否可以?何时不知道?

0 个答案:

没有答案