12345678910111213141516171819202122232425262728 |
- package J20250805.demo04;
- import java.lang.reflect.Constructor;
- /**
- * @author WanJl
- * @version 1.0
- * @title ClassTest02
- * @description
- * @create 2025/8/5
- */
- public class ClassTest02 {
- public static void main(String[] args) throws NoSuchMethodException {
- Class<Student> studentClass = Student.class;
- //通过Class类获取某个类的所有【公共的】构造方法组成的数组
- Constructor<?>[] constructors = studentClass.getConstructors();
- System.out.println(constructors.length);
- //通过Class类获取某个类的所有的构造方法组成的数组
- Constructor<?>[] declaredConstructors = studentClass.getDeclaredConstructors();
- System.out.println(declaredConstructors.length);
- //通过Class类获取某个公共构造方法的对象
- Constructor<Student> constructor = studentClass.getConstructor(String.class, String.class, Integer.class, String.class);
- System.out.println(constructor);
- //通过Class类获取某个构造方法的对象
- Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor(String.class);
- System.out.println(declaredConstructor);
- }
- }
|