【发布时间】:2018-03-20 13:48:56
【问题描述】:
由于我自学过编程,我认为我应该开始创建适当的类和变量,但我似乎不太明白。
我已经读过,首先我需要一个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
其次,如果我需要通过将多个
Pitches添加到一个列表中来制作一个words list的音高(例如,将 6 个不同的音高添加到一个列表中),然后将这些Pitches添加到另一个列表中将是新列表的第一个元素。那我是否需要创建另一个名为Word的class并在那里定义什么是单词以及如何设置/获取它?第三,我从 cmets 中了解到这里不需要使用 Interface。我什么时候知道可以使用接口,什么时候不可以?
【问题讨论】:
-
每次调用
makeListOfPitches(pitch, pitchString);时,您都在传递 同一个 Pitch 对象。因此,您正在向您的 同一个对象添加多个引用清单,一遍又一遍。您需要在循环的每次迭代中添加一个全新的 Pitch 对象。 -
这么简单的代码,不,不需要接口。您通常不会为单个类定义接口,但是当您想要跨多个类的通用行为时
-
@VGR 哦.. 是的,一旦我将它添加到函数
getListOfPitches()的循环内,它就会产生不同的音调。谢谢 -
从
Foo的字段中删除Pitch pitch = new Pitch(); -
应该根据实现它的类是否有不同的行为来决定何时使用接口。如果你不这样做,我认为没有理由使用界面。
标签: java class interface object-oriented-analysis