一 点睛
当调用大量方法,可以使用反射和注解简化编程。
二 代码
import java.lang.annotation.Annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.Method;import java.util.ArrayList;@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@interface testAnnoation10 { public String name() default "methodname"; public String unit() default "unit";}public class ch21_10 { public static void main( String[] args ) throws Exception { ch21_10 ch9 = new ch21_10(); Method method[] = ch9.getClass().getMethods(); for (Method method2 : method) { Annotation annotation = method2.getAnnotation(testAnnoation10.class); Class> ts[] = method2.getParameterTypes(); if (method2.getName().indexOf("getData") == -1) continue; ArrayList
三 运行
Tempreture is 20 AFElectricity is 100 AhSOC is 10 %