Topic: Tiger and enum |
Print this page |
1.Tiger and enum | Copy to clipboard |
Posted by: zerol Posted on: 2003-12-27 01:05 Hi all, I downloaded jdk1.5( Tiger) and tried it to compile an enum example in http://java.sun.com/features/2003/05/bloch_qa.html but failed, anyone tell me why? the two classes and compile time errors are listed below. TIA. public enum Coin { penny(1), nickel(5), dime(10), quarter(25); Coin(int value) { this.value = value; } private final int value; public int value() { return value; } } public class CoinTest { public static void main(String[] args) { for (Coin c : Coin.VALUES) System.out.println(c + ": \t" + c.value() +"¢ \t" + color); } private enum CoinColor { copper, nickel, silver } private static CoinColor color(Coin c) { switch { case Coin.penny: return CoinColor.copper; case Coin.nickel: return CoinColor.nickel; case Coin.dime: case Coin.quarter: return CoinColor.silver; default: throw new AssertionError("Unknown coin: " + c); } } } D:\j2sdk1.5.0\bin>javac -source 1.5 CoinTest.java CoinTest.java:3: cannot find symbol symbol : variable VALUES location: class Coin for (Coin c : Coin.VALUES) ^ CoinTest.java:10: an enum switch case label must be the unqualified name of an enumeration constant case Coin.penny: return CoinColor.copper; ^ CoinTest.java:11: an enum switch case label must be the unqualified name of an enumeration constant case Coin.nickel: return CoinColor.nickel; ^ CoinTest.java:11: duplicate case label case Coin.nickel: return CoinColor.nickel; ^ CoinTest.java:12: an enum switch case label must be the unqualified name of an enumeration constant case Coin.dime: ^ CoinTest.java:12: duplicate case label case Coin.dime: ^ CoinTest.java:13: an enum switch case label must be the unqualified name of an enumeration constant case Coin.quarter: return CoinColor.silver; ^ CoinTest.java:13: duplicate case label case Coin.quarter: return CoinColor.silver; ^ 8 errors |
2.Re:Tiger and enum [Re: zerol] | Copy to clipboard |
Posted by: toString Posted on: 2003-12-27 03:43 我这边也是一样有这个问题,不知道是不是Tiger alpha后来变了,而文档没有更新。因为采访Joshua bloch的那边文章是今年五月,已经过去七个月了。Anything might changed. |
3.Re:Tiger and enum [Re: zerol] | Copy to clipboard |
Posted by: 米国老 Posted on: 2003-12-29 15:39 改成下面这样就ok啦。 public class CoinTest { public static void main(String[] args) { for (Coin c : Coin.class.getEnumConstants()) System.out.println(c + ": \t" + c.value() +" Color \t" + color); } private enum CoinColor { copper, nickel, silver } private static CoinColor color(Coin c) { switch { case penny: return CoinColor.copper; case nickel: return CoinColor.nickel; case dime: case quarter: return CoinColor.silver; default: throw new AssertionError("Unknown coin: " + c); } } } |
4.Re:Tiger and enum [Re: 米国老] | Copy to clipboard |
Posted by: wsj Posted on: 2003-12-29 16:19 Yeah, got it worked. Thanks |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |