java enum 关联_Java:如果EnUM常量在本质上是静态的,那么它如何可以拥有与其相关联的构造函数和方法...
如果枚舉常量本質上是靜態的,那么如何將其作為對象和訪問方法并具有構造函數。
枚舉常量如何同時是對象和靜態的?
請參考以下代碼:
enum Apple {
Jonathan(10), GoldenDel(9), RedDel, Winesap(15), Cortland(8);
private int price; // price of each apple
// Constructor
Apple(int p) {
System.out.println("Price: " + p);
price = p;
}
// default constructor, constructor overloading
Apple() {
price = -1;
System.out.println("Price: " + price);
}
int getPrice() { return price; }
}
class EnumDemo3 {
public static void main(String args[]) {
Apple ap;
// Display price of Winesap.
System.out.println("Winesap costs " + Apple.Winesap.getPrice() + " cents.\n");
// Display all apples and prices.
System.out.println("All apple prices:");
for(Apple a : Apple.values())
System.out.println(a + " costs " + a.getPrice() + " cents.");
}
}
總結
以上是生活随笔為你收集整理的java enum 关联_Java:如果EnUM常量在本质上是静态的,那么它如何可以拥有与其相关联的构造函数和方法...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ae中心点重置工具_AE脚本:锚点中心点
- 下一篇: grep的java源程序_Java实现G