getClasses和getDeclaredClasses的区别:
getClasses得到该类及其父类所有的public的内部类。
getDeclaredClasses得到该类所有的内部类,除去父类的。
public class SpiMain { private class SpiMainPrivate { } public class SpiMainPublic { } } public class Main extends SpiMain{ private class MainPrivate { } public class MainPublic { } } 运行如下代码: System.out.println(Arrays.toString(Main.class.getClasses())); System.out.println(Arrays.toString(Main.class.getDeclaredClasses()));
输出:
[class anno.Main$MainPublic, class com.javartisan.spi.SpiMain$SpiMainPublic]
[class anno.Main$MainPrivate, class anno.Main$MainPublic]
对于注解
import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface Component { } import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Component @Retention(RetentionPolicy.RUNTIME) public @interface Service { }
java.lang.Class#getDeclaredAnnotation方法可以获取Service注解之上的Comnponent注解
原文地址:https://www.cnblogs.com/leodaxin/p/9594071.html
时间: 2024-10-09 07:40:31