【发布时间】:2016-04-21 21:06:45
【问题描述】:
我正在像这样的类中创建一些静态对象。我希望它们保持不变,但似乎我做错了,因为所有对象 a b c 都是相同的(它们的 x 是相同的)。谁能解释为什么?
public class Key {
private Note[] sequence;
private String name;
public Key() {
this.sequence = Note.NOTES;
}
public Key(String name, Note[] notes) {
this.sequence = Note.NOTES;
this.name = name;
for(int i = 0; i < this.sequence.length; i++){
for(int j = 0; j < notes.length; j++){
if(this.sequence[i].equals(notes[j])){
this.sequence[i].setIntensity(0.6);
break;
}
else this.sequence[i].setIntensity((double)0);
}
}
}
private static final Key C = new Key("C", new Note[]{Note.F2, Note.G2, Note.A2, Note.B2, Note.C3, Note.D3, Note.E3, Note.F3, Note.G3, Note.A3, Note.B3});
private static final Key CSharp = new Key("C#", new Note[]{Note.Fsharp2, Note.Gsharp2, Note.Asharp2, Note.C3, Note.Csharp3, Note.Dsharp3, Note.F3, Note.Fsharp3, Note.Gsharp3, Note.Asharp3});
private static final Key D = new Key("D", new Note[]{Note.Fsharp2, Note.G2, Note.A2, Note.B2, Note.Csharp3, Note.D3, Note.E3, Note.Fsharp3, Note.G3, Note.A3, Note.B3});
private static final Key EFlat = new Key("Eb", new Note[]{Note.F2, Note.G2, Note.Gsharp2, Note.Asharp2, Note.C3, Note.D3, Note.Dsharp3, Note.F3, Note.G3, Note.Gsharp3, Note.Asharp3});
private static final Key E = new Key("E", new Note[]{Note.Fsharp2, Note.Gsharp2, Note.A2, Note.B2, Note.Csharp3, Note.Dsharp3, Note.E3, Note.Fsharp3, Note.Gsharp3, Note.A3, Note.B3});
private static final Key F = new Key("F", new Note[]{Note.F2, Note.G2, Note.A2, Note.Asharp2, Note.C3, Note.D3, Note.E3, Note.F3, Note.G3, Note.A3, Note.Asharp3});
private static final Key FSharp = new Key("F#", new Note[]{Note.Fsharp2, Note.Gsharp2, Note.Asharp2, Note.B2, Note.Csharp3, Note.Dsharp3, Note.F3, Note.Fsharp3, Note.Gsharp3, Note.Asharp3, Note.B3});
private static final Key G = new Key("G", new Note[]{Note.Fsharp2, Note.G2, Note.A2, Note. B2, Note.C3, Note.D3, Note.E3, Note.Fsharp3, Note.G3, Note.A3, Note.B3});
private static final Key GSharp = new Key("G#", new Note[]{Note.F2, Note.G2, Note.Gsharp2, Note.Asharp2, Note.C3, Note.Csharp3, Note.Dsharp3, Note.F3, Note.G3, Note.Gsharp3, Note.Asharp3});
private static final Key A = new Key("A", new Note[]{Note.Fsharp2, Note.Gsharp2, Note.A2, Note.B2, Note.Csharp3, Note.Dsharp3, Note.E3, Note.Fsharp3, Note.Gsharp3, Note.A3, Note.B3});
private static final Key BFlat = new Key("Bb", new Note[]{Note.F2, Note.G2, Note.A2, Note.Asharp2, Note.C3, Note.D3, Note.Dsharp3, Note.F3, Note.G3, Note.A3, Note.Asharp3});
private static final Key B = new Key("B", new Note[]{Note.Fsharp2, Note.Gsharp2, Note.Asharp2, Note.B2, Note.Csharp3, Note.Dsharp3, Note.E3, Note.Fsharp3, Note.Gsharp3, Note.Asharp3, Note.B3});
}
【问题讨论】:
-
没有。
x的值会有所不同。你在哪里试过打印这些? -
我确实简化了我的代码,但我认为它完全一样。我不知道为什么,但它使所有属性都变得相同
-
向我们展示完整的代码。
-
到底发生了什么?
-
所有静态对象的属性顺序相同。他们取最后一个对象(Key.B)的值