"需求:定义一个ClothType枚举类,表示衣服分类的三种情况:男装、女装、中性服装\n\n并在测试类中获取枚举中的常量对象,并调用枚举中的name、ordinal方法。\n\n作业目的:定义枚举类和枚举的使用内容:java\npublic class EnumExample {\n public enum ClothType {\n 男装, 女装, 中性服装\n }\n \n public static void main(String[] args) {\n ClothType clothType = ClothType.男装;\n \n System.out.println(clothType.name()); // 输出:男装\n System.out.println(clothType.ordinal()); // 输出:0\n \n clothType = ClothType.女装;\n \n System.out.println(clothType.name()); // 输出:女装\n System.out.println(clothType.ordinal()); // 输出:1\n \n clothType = ClothType.中性服装;\n \n System.out.println(clothType.name()); // 输出:中性服装\n System.out.println(clothType.ordinal()); // 输出:2\n }\n}\n\n

Java 枚举类示例:衣服分类 - 定义、使用及常量访问

原文地址: https://www.cveoy.top/t/topic/pTSa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录