如何实现一个enumeration类(内详)?
看Thinking in Java时看到下面这样一段程序:
//: c07:music:Note.java
// Notes to play on musical instruments.
package c07.music;
import com.bruceeckel.simpletest.*;
public class Note {
private String noteName;
private Note(String noteName) {
this.noteName = noteName;
}
public String toString() { return noteName; }
public static final Note
MIDDLE_C = new Note("Middle C"),
C_SHARP = new Note("C Sharp"),
B_FLAT = new Note("B Flat");
// Etc.
} ///:~
这段程序实现了一个枚举类Note,它不能生成新的对象(因为ctor是私有的),只能用它固有的三个static对象。
这样的一个类在C++中如何来实现呢?
我的想法是在类中用指针或引用来实现,但在细节的语法层面上还有一些不太清楚的地方,请高手指点一下。谢谢!