java之注解、枚举理解(高效java之注解、枚举)

   记录学习的笔记 多说无益 都在注释和代码里

/**
 * enum枚举和注解的学习笔记  
 * 枚举测试用例-->testEnumClass
 * 枚举类型-->有一组固定的常量组成合法值的类型
 * 枚举类型提供了编译时的类型安全
 * 枚举天生就是不可变的,因此所有域都应该是final
 * 枚举常量实际上是static final的常量
 * 特定于常量的类主体--枚举的抽象方法
 * 虽然无法编写可扩展的枚举类型,但是可以通过接口的方式为枚举模拟扩展
 * 测试位域  用例-->testEnumSet
 * 测试Map  用例-->testEnumMap
 * 
 * 注解
 * 元注解---->注解类型声明中的注解  private @interface TestSome
 * 标记注解---->标注被注解的元素    @TestSome
 * 解析注解---对标记注解按照元注解的实现进行解析  测试用例-->testSome
 * 坚持使用Override注解,表明此方法是覆盖超类型中的一个声明,也可防止重载Overload
 * 标记接口---->没有包含方法声明的接口,只是指明类实现了具有某种属性的接口(特定属性)
 *  Cloneable、Serializable等
 *  如果想要使用类型,一定要定义接口
 * 
 * @author Administrator
 *
 */

测试代码

package com.undergrowth.lang;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

/**
 * enum枚举和注解的学习笔记  
 * 枚举测试用例-->testEnumClass
 * 枚举类型-->有一组固定的常量组成合法值的类型
 * 枚举类型提供了编译时的类型安全
 * 枚举天生就是不可变的,因此所有域都应该是final
 * 枚举常量实际上是static final的常量
 * 特定于常量的类主体--枚举的抽象方法
 * 虽然无法编写可扩展的枚举类型,但是可以通过接口的方式为枚举模拟扩展
 * 测试位域  用例-->testEnumSet
 * 测试Map  用例-->testEnumMap
 * 
 * 注解
 * 元注解---->注解类型声明中的注解  private @interface TestSome
 * 标记注解---->标注被注解的元素    @TestSome
 * 解析注解---对标记注解按照元注解的实现进行解析  测试用例-->testSome
 * 坚持使用Override注解,表明此方法是覆盖超类型中的一个声明,也可防止重载Overload
 * 标记接口---->没有包含方法声明的接口,只是指明类实现了具有某种属性的接口(特定属性)
 *  Cloneable、Serializable等
 *  如果想要使用类型,一定要定义接口
 * 
 * @author Administrator
 *
 */
public class EnumAnnotationLearn {

	public EnumAnnotationLearn() {
		// TODO Auto-generated constructor stub
	}
	
	@Test
	public void testEnumClass(){
		//枚举类型自动产生的valueOf(String) ,将常量的名字转为常量本身
		System.out.println(EnumClass.ADD);
		//values()按照声明顺序返回它的值数组
		System.out.println("values()-->按照声明顺序返回它的值数组");
		System.out.println("操作数10.0和5.0");
		for(EnumClass ec:EnumClass.values()){
			System.out.print(ec+"\t序列号:"+ec.ordinal()+"\t"+"代表符号:"+ec.getName()+"\t");
			System.out.println(String.valueOf(ec.apply(10.0, 5.0)));
			
		}
		
	}
	
	private enum EnumClass{
		ADD("+") {
			@Override
			Double apply(Double op1, Double op2) {
				// TODO Auto-generated method stub
				return op1+op2;
			}
		},MINUS("-") {
			@Override
			Double apply(Double op1, Double op2) {
				// TODO Auto-generated method stub
				return op1-op2;
			}
		},MULTI("*") {
			@Override
			Double apply(Double op1, Double op2) {
				// TODO Auto-generated method stub
				return op1*op2;
			}
		},DIVIDE("/") {
			@Override
			Double apply(Double op1, Double op2) {
				// TODO Auto-generated method stub
				return op1/op2;
			}
		};
		
		private final String name;
		EnumClass(String name){
			this.name=name;
		}
		//枚举的抽象方法 就是特定于常量的类主体
		abstract Double apply(Double op1,Double op2);
		public String getName() {
			return name;
		}
		
		
	}

	/**
	 * 测试位域
	 * 使用EnumSet替代位域
	 */
	@Test
	public void testEnumSet(){
		Set<EnumClass> enumSet=EnumSet.of(EnumClass.ADD, EnumClass.MINUS);
		for (EnumClass enumClass : enumSet) {
			System.out.println(enumClass.getName()+"\t"+enumClass.ordinal());
		}
	}
	
	/**
	 * 测试枚举类型键
	 * 使用EnumMap替代序列
	 */
	@Test
	public void testEnumMap(){
		Map<EnumClass, String> enmuMap=new EnumMap<>(EnumClass.class);
		enmuMap.put(EnumClass.DIVIDE, "除数不能为0");
		for (Map.Entry<EnumClass, String> entry : enmuMap.entrySet()) {
			System.out.println(entry.getKey().getName()+"\t"+entry.getValue());
		}
	}
	
	/**
	 * 元注解
	 * Retention-->表示注解应该在什么时候保留
	 * Target-->表示注解应该应用的范围
	 * 此注解只能用于无参数的静态方法上,用于添加欢迎信息
	 * @author Administrator
	 *
	 */
	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.METHOD)
	private @interface TestSome{}
	
	@Test
	public void testSome(){
		System.out.println("测试注解\t"+TestSome.class);
		Class<AnnotationTest> anClass=AnnotationTest.class;
		int countAnno=0,pass=0;
		for(Method method:anClass.getDeclaredMethods()){
			if(method.isAnnotationPresent(TestSome.class)){
				countAnno++;
				try {
					method.invoke(null);
					//可以添加更多的功能
					System.out.println("你使用了欢迎注解,并且成功调用,谢谢使用");
					pass++;
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
		System.out.println("总共有注解方法"+countAnno+"\t通过测试注解方法"+pass);
	}
	
	public static class AnnotationTest{
		@TestSome
		public static void hello(){
			System.out.println(AnnotationTest.class.getName()+"\t hello world annotation");
		}
		@TestSome
		public void hello(String name){
			System.out.println(AnnotationTest.class.getName()+"\t "+name+" annotation");
		}
		
		public void hello(String name,String sex){
			System.out.println(AnnotationTest.class.getName()+"\t "+name+sex+" annotation");
		}
	}
	
}


控制台输出

测试注解	interface com.undergrowth.lang.EnumAnnotationLearn$TestSome
com.undergrowth.lang.EnumAnnotationLearn$AnnotationTest	 hello world annotation
你使用了欢迎注解,并且成功调用,谢谢使用
java.lang.NullPointerException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.undergrowth.lang.EnumAnnotationLearn.testSome(EnumAnnotationLearn.java:147)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)总共有注解方法2	通过测试注解方法1

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
ADD
values()-->按照声明顺序返回它的值数组
操作数10.0和5.0
ADD	序列号:0	代表符号:+	15.0
MINUS	序列号:1	代表符号:-	5.0
MULTI	序列号:2	代表符号:*	50.0
DIVIDE	序列号:3	代表符号:/	2.0
+	0
-	1
/	除数不能为0



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值