[Java]Enumで遊んでみる
- January 28th, 2010
- Posted in Java
- Write comment
長らくJavaをやってなかったら、Enumとかいうのができていたので使ってみた。
使い方あっているのだろうか…
// for later than 1.5
enum KtaiInfoEnum {
DOCOMO(0, "docomo"),
SOFTBANK(1, "softbank"),
AU(2, "au");
private String carrierName = null;
private Integer type = 0;
private KtaiInfoEnum(Integer type, String carrierName) {
this.type = type;
this.carrierName = carrierName;
}
public Integer getType() {
return this.type;
}
public String getCarrierName() {
return this.carrierName;
}
}
// for java 1.4
class KtaiInfo {
public static final KtaiInfo DOCOMO = new KtaiInfo(0, "docomo");
public static final KtaiInfo SOFTBANK = new KtaiInfo(1, "softbank");
public static final KtaiInfo AU = new KtaiInfo(2, "au");
private String carrierName = null;
private Integer type = null;
private KtaiInfo(Integer type, String carrierName) {
this.type = type;
this.carrierName = carrierName;
}
public Integer getType() {
return this.type;
}
public String getCarrierName() {
return this.carrierName;
}
}
public class TestListStructure {
public static void main(String args[]) {
System.out.println(KtaiInfo.SOFTBANK.getType() + ":" + KtaiInfo.SOFTBANK.getCarrierName());
System.out.println(KtaiInfoEnum.SOFTBANK.getType() + ":" + KtaiInfoEnum.SOFTBANK.getCarrierName());
for(KtaiInfoEnum ktaiInfo: KtaiInfoEnum.values()){
System.out.println(ktaiInfo.getType() + ":" + ktaiInfo.getCarrierName());
}
}
}
Popularity: 15% [?]
No comments yet.