1.获取构造方法
构造方法在 反射中的类是
Constructor
!
1.0 先准备一个类
package com.northcastle.groupconstructor;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Dog {
private String name;
private Integer age;
//准备一批构造方法
public Dog() {
}
private Dog(String name) {
this.name = name;
}
public Dog(Integer age) {
this.age = age;
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
}
1.1 获取所有的构造方法列表
getConstructors() : 获取所有的 public 的构造方法
getDeclaredConstructors() : 获取所有的构造方法
package com.northcastle.groupconstructor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Application {
public static void main(String[] args) throws ClassNotFoundException {
//1.创建Class类的对象
String classPath = "com.northcastle.groupconstructor.Dog";
Class<?> clazz = Class.forName(classPath);
//2.获取所有的构造器的列表
Constructor<?>[] constructorList = clazz.getConstructors();
Constructor<?>[] declaredConstructorList = clazz.getDeclaredConstructors();
for (Constructor<?> constructor : constructorList) {
System.out.println(constructor);
}
System.out.println("======================");
for (Constructor<?> constructor : declaredConstructorList) {
System.out.println(constructor);
}
}
}
#运行结果
public com.northcastle.groupconstructor.Dog(java.lang.String,java.lang.Integer)
public com.northcastle.groupconstructor.Dog(java.lang.Integer)
public com.northcastle.groupconstructor.Dog()
======================
public com.northcastle.groupconstructor.Dog(java.lang.String,java.lang.Integer)
public com.northcastle.groupconstructor.Dog(java.lang.Integer)
private com.northcastle.groupconstructor.Dog(java.lang.String)
public com.northcastle.groupconstructor.Dog()
1.2 获取指定的构造方法
getConstructor(参数类型列表) :获取指定的public 的构造方法;
getDeclaredConstructor(参数类型列表) :获取指定的构造方法
package com.northcastle.groupconstructor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Application {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//1.创建Class类的对象
String classPath = "com.northcastle.groupconstructor.Dog";
Class<?> clazz = Class.forName(classPath);
//3.获取指定的构造方法
Constructor<?> constructor = clazz.getConstructor(Integer.class);
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class);
System.out.println(constructor);
System.out.println(declaredConstructor);
}
}
# 运行结果
public com.northcastle.groupconstructor.Dog(java.lang.Integer)
private com.northcastle.groupconstructor.Dog(java.lang.String)
1.3 获取构造方法的修饰符
getModifiers() : 以int形式返回修饰符
【default = 0 ; public = 1; private = 2; protected = 4; static = 8;final = 16】
package com.northcastle.groupconstructor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Application {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//1.创建Class类的对象
String classPath = "com.northcastle.groupconstructor.Dog";
Class<?> clazz = Class.forName(classPath);
//4.获取构造方法的修饰符
Constructor<?> declaredConstructor2 = clazz.getDeclaredConstructor(String.class);
int modifiers = declaredConstructor2.getModifiers();
System.out.println(declaredConstructor2); // private = 2
System.out.println(modifiers);
}
}
# 运行结果
private com.northcastle.groupconstructor.Dog(java.lang.String)
2
1.4 获取构造方法的全类名
getName()
package com.northcastle.groupconstructor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Application {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//1.创建Class类的对象
String classPath = "com.northcastle.groupconstructor.Dog";
Class<?> clazz = Class.forName(classPath);
//5.获取构造方法的全类名
Constructor<?> declaredConstructor3 = clazz.getDeclaredConstructor(String.class);
String name = declaredConstructor3.getName();
System.out.println(name);
}
}
# 运行结果
com.northcastle.groupconstructor.Dog
1.5 获取构造方法的参数列表
getParameterTypes() : 以Class[] 类型返回构造方法的参数列表
package com.northcastle.groupconstructor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Application {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//1.创建Class类的对象
String classPath = "com.northcastle.groupconstructor.Dog";
Class<?> clazz = Class.forName(classPath);
//6.获取构造方法的参数列表
Constructor[] list = clazz.getDeclaredConstructors();
for (Constructor constructor1 : list) {
Class[] parameterTypes = constructor1.getParameterTypes();
System.out.println(constructor1);
System.out.println(Arrays.toString(parameterTypes));
System.out.println("***");
}
}
}
# 运行结果
public com.northcastle.groupconstructor.Dog(java.lang.String,java.lang.Integer)
[class java.lang.String, class java.lang.Integer]
***
public com.northcastle.groupconstructor.Dog(java.lang.Integer)
[class java.lang.Integer]
***
private com.northcastle.groupconstructor.Dog(java.lang.String)
[class java.lang.String]
***
public com.northcastle.groupconstructor.Dog()
[]
***
1.6 使用构造方法创建对象
newInstance(构造方法的参数) :
package com.northcastle.groupconstructor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
/**
* author : northcastle
* createTime:2021/10/13
*/
public class Application {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//1.创建Class类的对象
String classPath = "com.northcastle.groupconstructor.Dog";
Class<?> clazz = Class.forName(classPath);
//7.使用构造方法创建对象
Constructor<?> declaredConstructor1 = clazz.getDeclaredConstructor(String.class, Integer.class);
Object aa = declaredConstructor1.newInstance("aa", 3);
Class<?> aClass = aa.getClass(); // 创建的对象的运行时类型
System.out.println(aClass);
if (aa instanceof Dog){
System.out.println("对象aa的确是Dog类型");
Dog d = (Dog) aa;
System.out.println(d);
}else{
System.out.println("对象aa不是Dog类型");
}
}
}
# 运行结果
class com.northcastle.groupconstructor.Dog
对象aa的确是Dog类型
Dog{name='aa', age=3}
2.完成
Congratulations!
You are one step closer to success!