OpConstants.java
public enum OpConstants {TURN_LEFT, TURN_RIGHT, SHOOT}
ShowEnum.java
public class ShowEnum {
public static void main(String[] args) {
for(OpConstants constant: OpConstants.values()) {
System.out.println(constant.toString());
}
}
}
TURN_LEFT TURN_RIGHT SHOOT
ShowEnum.java
public class ShowEnum {
public static void main(String[] args) {
enumCompareTo(OpConstants.valueOf(args[0]));
}
public static void enumCompareTo(OpConstants constant) {
System.out.println(constant);
for(OpConstants c: OpConstants.values()) {
System.out.println(constant.compareTo(c));
}
}
}
$java ShowEnum TURN_RIGHT
TURN_RIGHT 1 0 -1
ShowEnum.java
public class ShowEnum {
public static void main(String[] args) {
for(OpConstants c : OpConstants.values()) {
System.out.printf("%d %s%n", c.ordinal(), c);
}
}
}
0 TURN_LEFT 1 TURN_RIGHT 2 SHOOT
class TestClass {
private TestClass(){}//定义私有构造函数,不能从外部实例化对象
//提供两个实例A、B
public static final TestClass A=new TestClass();
public static final TestClass B=new TestClass();
}
enum TestClass01{
A,B;
}
enum TestClass02{
A("a") //建立对象时传入构造函数参数
,B("b");
private String value;
private TestClass02(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}
enum TestClass03{
A(){
//实现抽象方法
public void type(){
System.out.println("优");
}
}
,B(){
public void type(){
System.out.println("良");
}
};
public abstract void type();//定义抽象方法
}
public class Test02 {
public static void main(String[] args){
print(TestClass02.A);//传入枚举参数
}
public static void print(TestClass02 t){
System.out.println(t.getValue());
}
enum TestClass02{
A("a")
,B("b");
private String value;
private TestClass02(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}
}
a Author:事始 Sign:只要你还在尝试,就不算失败。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有