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());
}
}
首先,如果我在循环中打印出所有 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 个不同的音高添加到 1 个列表中)然后添加那些 Pitches
进入另一个列表,这将是新列表的第一个元素。那么我是否需要创建另一个名为 Word
的 class
并在那里定义什么词以及如何 set/get 它?
第三我看评论说这里不需要使用Interface。我什么时候知道可以使用接口,什么时候不能?
回答你问题的最后一部分,在这里使用接口没有好坏之分,你实际上是在为你的getters和setters创建一个接口,是否有必要取决于你想如何使用接口。看起来你的情况没有必要。看看Java Interface Usage Guidelines -- Are getters and setters in an interface bad?
关于你的第二个问题,你可能也不需要单独的 class 来实例化音高列表,word
是否真的有意义并且会在其他地方实例化为对象?如果您只想要一个列表,只需按照上面 List<Pitch> word = new ArrayList<Pitch>();
所做的操作,然后将球场添加到列表中即可。
自从我自学了编程,我想我应该开始创建适当的 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());
}
}
首先,如果我在循环中打印出所有
获得的不同值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 个不同的音高添加到 1 个列表中)然后添加那些Pitches
进入另一个列表,这将是新列表的第一个元素。那么我是否需要创建另一个名为Word
的class
并在那里定义什么词以及如何 set/get 它?第三我看评论说这里不需要使用Interface。我什么时候知道可以使用接口,什么时候不能?
回答你问题的最后一部分,在这里使用接口没有好坏之分,你实际上是在为你的getters和setters创建一个接口,是否有必要取决于你想如何使用接口。看起来你的情况没有必要。看看Java Interface Usage Guidelines -- Are getters and setters in an interface bad?
关于你的第二个问题,你可能也不需要单独的 class 来实例化音高列表,word
是否真的有意义并且会在其他地方实例化为对象?如果您只想要一个列表,只需按照上面 List<Pitch> word = new ArrayList<Pitch>();
所做的操作,然后将球场添加到列表中即可。