Java Annotatation反射与.NET Attribute的稍稍不同
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface ClassInfo {
String created();
String createdBy();
String lastModified();
String lastModifiedBy();
int revision() default 0;
}
@ClassInfo (
created = "Jan 31 2005",
createdBy = "James Gosling",
lastModified = "Feb 9 2005",
lastModifiedBy = "Ken Arnold",
revision = 3
)
class Foo {
}
public class ClientConsole{
public static void main(String[] args) {
if(Foo.class.isAnnotationPresent(ClassInfo.class)){
System.out.println("Annotatted");
ClassInfo info = Foo.class.getAnnotation(ClassInfo.class);
System.out.println(info.created());
}
else{
System.out.println("not Annotatted");
}
System.out.println("Finished ...");
}
}
只有标注为可以运行态反射才能够访问,这点与Attribue有较大不同。
(不适应,找了2个小时才找到问题。)
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface ClassInfo {
String created();
String createdBy();
String lastModified();
String lastModifiedBy();
int revision() default 0;
}
@ClassInfo (
created = "Jan 31 2005",
createdBy = "James Gosling",
lastModified = "Feb 9 2005",
lastModifiedBy = "Ken Arnold",
revision = 3
)
class Foo {
}
public class ClientConsole{
public static void main(String[] args) {
if(Foo.class.isAnnotationPresent(ClassInfo.class)){
System.out.println("Annotatted");
ClassInfo info = Foo.class.getAnnotation(ClassInfo.class);
System.out.println(info.created());
}
else{
System.out.println("not Annotatted");
}
System.out.println("Finished ...");
}
}
只有标注为可以运行态反射才能够访问,这点与Attribue有较大不同。
(不适应,找了2个小时才找到问题。)
贸易电子化,技术全球化