ClassTest02.java 1.2 KB

12345678910111213141516171819202122232425262728
  1. package J20250805.demo04;
  2. import java.lang.reflect.Constructor;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title ClassTest02
  7. * @description
  8. * @create 2025/8/5
  9. */
  10. public class ClassTest02 {
  11. public static void main(String[] args) throws NoSuchMethodException {
  12. Class<Student> studentClass = Student.class;
  13. //通过Class类获取某个类的所有【公共的】构造方法组成的数组
  14. Constructor<?>[] constructors = studentClass.getConstructors();
  15. System.out.println(constructors.length);
  16. //通过Class类获取某个类的所有的构造方法组成的数组
  17. Constructor<?>[] declaredConstructors = studentClass.getDeclaredConstructors();
  18. System.out.println(declaredConstructors.length);
  19. //通过Class类获取某个公共构造方法的对象
  20. Constructor<Student> constructor = studentClass.getConstructor(String.class, String.class, Integer.class, String.class);
  21. System.out.println(constructor);
  22. //通过Class类获取某个构造方法的对象
  23. Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor(String.class);
  24. System.out.println(declaredConstructor);
  25. }
  26. }