【Java基础】枚举和注解
2017-04-26 09:57
477 查看
在Java1.5版本中,引入了两个类型:枚举类型
枚举类型
示例代码:
总而言之,正是因为枚举类型要用在集合
命名模式有两个缺点:
文字拼写错误会导致失败,且没有任何提示。
无法确保它们只用于相应的程序元素上。
它们没有提供将参数值与程序元素关联起来的好方法。
针对以上几个问题,注解很好地解决了所有这些问题。
Test注解类型的声明就是它自身通过
那么声明的Test注解,则称作为”标记注解”
示例代码:
另外一种注解方式:
使用方式
enum type和注解类型
annotation type。
Num1:用enum代替int常量
枚举类型enum type是指由一组固定的常量组成合法值的类型。比如:
public enum Planet { MERCURY(3.302e+23, 2.439e6), VENUS(4.869e+24, 6.052e6), EARTH(5.975e+24, 6.378e6), MARS(6.419e+23, 3.393e6), JUPITER(1.899e+27, 7.149e7), SATURN( 5.685e+26, 6.027e7), URANUS(8.683e+25, 2.556e7), NEPTUNE(1.024e+26, 2.477e7); private final double mass; // In kilograms private final double radius; // In meters private final double surfaceGravity; // In m / s^2 // Universal gravitational constant in m^3 / kg s^2 private static final double G = 6.67300E-11; // Constructor Planet(double mass, double radius) { this.mass = mass; this.radiu 4000 s = radius; surfaceGravity = G * mass / (radius * radius); } public double mass() { return mass; } public double radius() { return radius; } public double surfaceGravity() { return surfaceGravity; } public double surfaceWeight(double mass) { return mass * surfaceGravity; // F = ma } } public class WeightTable { public static void main(String[] args) { double earthWeight = Double.parseDouble(args[0]); double mass = earthWeight / Planet.EARTH.surfaceGravity(); for (Planet p : Planet.values()) System.out.printf("Weight on %s is %f%n", p, p.surfaceWeight(mass)); } }
Num2:用EnumSet代替位域
示例代码:public class Text { public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH } // Any Set could be passed in, but EnumSet is clearly best public void applyStyles(Set<Style> styles) { // Body goes here } // Sample use public static void main(String[] args) { Text text = new Text(); text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC)); } }
总而言之,正是因为枚举类型要用在集合
Set中,所以没有理由用位域来表示它。
Num3:注解优先于命名模式
命名模式有两个缺点:文字拼写错误会导致失败,且没有任何提示。
无法确保它们只用于相应的程序元素上。
它们没有提供将参数值与程序元素关联起来的好方法。
针对以上几个问题,注解很好地解决了所有这些问题。
/** * Indicates that the annotated method is a test method. Use only on * parameterless static methods. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Test { }
Test注解类型的声明就是它自身通过
Retention和
Target注解进行了注解。注解类型声明中的这张注解被称作”元注解“
meta-annotaition。
那么声明的Test注解,则称作为”标记注解”
marker annotation。因为它没有参数,只是“标注”被注解的元素。
示例代码:
public class Sample { @Test public static void m1() { } // Test should pass public static void m2() { } @Test public static void m3() { // Test Should fail throw new RuntimeException("Boom"); } public static void m4() { } @Test public void m5() { } // INVALID USE: nonstatic method public static void m6() { } @Test public static void m7() { // Test should fail throw new RuntimeException("Crash"); } public static void m8() { } }
另外一种注解方式:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ExceptionTest { Class<? extends Exception>[] value(); }
使用方式
public class Sample2 { @ExceptionTest(ArithmeticException.class) public static void m1() { // Test should pass int i = 0; i = i / i; } @ExceptionTest(ArithmeticException.class) public static void m2() { // Should fail (wrong exception) int[] a = new int[0]; int i = a[1]; } @ExceptionTest(ArithmeticException.class) public static void m3() { } // Should fail (no exception) // Code containing an annotation with an array parameter - Page 174 @ExceptionTest({ IndexOutOfBoundsException.class, NullPointerException.class }) public static void doublyBad() { List<String> list = new ArrayList<String>(); // The spec permits this method to throw either // IndexOutOfBoundsException or NullPointerException list.addAll(5, null); } }
相关文章推荐
- 【Java基础】枚举和注解
- 黑马程序员_java基础加强_静态导入_反射_枚举_注解_内省_泛型_代理
- Java基础加强_Eclipse、枚举、反射、注解、泛型、类加载器、动态代理
- Java基础学习笔记之枚举与注解
- java基础枚举和注解
- 黑马程序员_java_基础加强_静态导入_反射_枚举_注解_内省_泛型
- 黑马程序员:基础加强(可变参数、增强for、枚举、javabBean、BeanUtils、注解、泛型)
- 23-java基础加强(反射、泛型、注解、动态代理)
- 01-java基础加强,eclipse工具使用,快捷键,断点调试,单元测试,jdk5新特性,增强for循环,集合map,list,可变参数,枚举,反射,泛型
- Java编程思想学习笔记——枚举、注解
- Java基础加强---Annotation(注解)
- 黑马程序员 -java基础 类加载器注解内省
- 黑马程序员 22 Java基础加强-04-注解篇
- Annotation & Enum :Java中注解和枚举的使用
- 理解Java基础之注解Annotation
- java基础---->枚举
- java 基础加强 类的枚举
- java基础加强(三)(注解与泛型)
- Java基础加强枚举笔记
- Java语言基础--枚举