1. package com.atguigu.java3;
    2. import com.atguigu.java2.Person;
    3. import org.junit.Test;
    4. import java.lang.annotation.Annotation;
    5. import java.lang.reflect.Constructor;
    6. import java.lang.reflect.ParameterizedType;
    7. import java.lang.reflect.Type;
    8. /**
    9. * @author Dxkstart
    10. * @create 2021-06-07 19:24
    11. */
    12. public class OtherTest {
    13. /*
    14. 获取构造器结构
    15. */
    16. @Test
    17. public void test1() {
    18. Class<Person> clazz = Person.class;
    19. //getConstructors():获取当前运行时类中声明为public的构造器
    20. Constructor[] constructors = clazz.getConstructors();
    21. for (Constructor cons : constructors) {
    22. System.out.println(cons);
    23. }
    24. System.out.println();
    25. //getDeclaredConstructors():获取当前运行时类中声明的所有构造器
    26. Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
    27. for (Constructor c : declaredConstructors) {
    28. System.out.println(c);
    29. }
    30. }
    31. /*
    32. 获取运行时类的父类
    33. */
    34. @Test
    35. public void test2() {
    36. Class<Person> clazz = Person.class;
    37. Class superclass = clazz.getSuperclass();
    38. System.out.println(superclass);
    39. }
    40. /*
    41. 获取运行时类的带泛型的父类
    42. */
    43. @Test
    44. public void test3() {
    45. Class<Person> clazz = Person.class;
    46. Type genericSuperclass = clazz.getGenericSuperclass();
    47. System.out.println(genericSuperclass);
    48. }
    49. /*
    50. 获取运行时类的带泛型的父类的泛型
    51. */
    52. @Test
    53. public void test4() {
    54. Class<Person> clazz = Person.class;
    55. Type genericSuperclass = clazz.getGenericSuperclass();
    56. ParameterizedType paramType = (ParameterizedType) genericSuperclass;
    57. //获取泛型类型
    58. Type[] actualTypeArguments = paramType.getActualTypeArguments();
    59. System.out.println(actualTypeArguments[0].getTypeName());
    60. }
    61. /*
    62. 获取运行时类实现的接口
    63. */
    64. @Test
    65. public void test5(){
    66. Class<Person> clazz = Person.class;
    67. Class[] interfaces = clazz.getInterfaces();
    68. for(Class c : interfaces){
    69. System.out.println(c);
    70. }
    71. System.out.println();
    72. //获取运行时类的父类实现的接口
    73. Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
    74. for(Class c1: interfaces1){
    75. System.out.println(c1);
    76. }
    77. }
    78. /*
    79. 获取运行时类所在的包
    80. */
    81. @Test
    82. public void test6(){
    83. Class<Person> clazz = Person.class;
    84. Package pack = clazz.getPackage();
    85. System.out.println(pack);
    86. }
    87. /*
    88. 获取运行时类声明的注解
    89. */
    90. @Test
    91. public void test7(){
    92. Class<Person> clazz = Person.class;
    93. Annotation[] annotations = clazz.getAnnotations();
    94. for(Annotation a:annotations){
    95. System.out.println(a);
    96. }
    97. }
    98. }