反射(获取构造器、方法)
反射
反射:
1.获取class对象
方式1:
Class clazz=Class.forName("全限定名")
方式2:
Class clazz=类名.class;
方式3:
Class clazz=对象.getClass;
2.可以获取对应类的构造方法(了解)
Constructor con = clazz.getConstructor(Class .. paramClass);
Person p = (Person) con.newInstance(参数);
persion.java
package com.reflect;
public class Persion {
private Integer id;
private String username;
private String password;
public Persion() {
System.out.println("无参构造器");
}
public Persion(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
System.out.println("带参构造器");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
text01.java
package com.reflect;
import org.junit.Test;
import java.lang.reflect.*;
public class test01 {
@Test
public void run() throws Exception {
//获取class对象
Class clazz = Class.forName("com.reflect.Persion");
//获取无参构造器
Constructor con1 = clazz.getConstructor();
Persion p1 = (Persion) con1.newInstance();
//获取带参构造器
Constructor con2 = clazz.getConstructor(Integer.class,String.class,String.class);
Persion p2 = (Persion) con2.newInstance(1,"hjh","12345");
}
}
3.可以通过clazz创建一个对象(了解)
clazz.newInstance();//相当于调用的无参构造器
4.可以通过clazz获取所有的字段 getFiled()(了解中的了解)
5.可以通过clazz获取所有的方法
Method m = clazz.getMethod("sleep");//获取公共的方法
Method m = clazz.getDeclaredMethod("sleep");//获取任意的方法
注意:若是私有的方法 必须让该方法可以访问
m.setAccessible(true);
persion.java
package com.reflect;
public class Persion {
private Integer id;
private String username;
private String password;
public Persion() {
System.out.println("无参构造器");
}
public Persion(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
System.out.println("带参构造器");
}
public Persion( String username) {
this.username = username;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void sport() {
System.out.println("游泳");
}
public void sport(String username) {
System.out.println(username+"在游泳");
}
private void sleep() {
System.out.println("睡觉");
}
private String sleep(String username) {
return username+"在睡觉";
}
}
test02.java
package com.reflect;
import org.junit.Test;
import java.lang.reflect.*;
public class test02 {
@Test
public void run() throws Exception {
//获取class对象
Class clazz = Persion.class;
//获取public修饰的方法
Method m1 = clazz.getMethod("sport");
m1.invoke(clazz.newInstance());
//获取任意方法
Method m2 = clazz.getDeclaredMethod("sleep");
//授权给私有和受保护方法,使之可以被访问
m2.setAccessible(true);
//执行方法
m2.invoke(clazz.newInstance());
//执行有返回值的方法
Method m3 = clazz.getDeclaredMethod("sleep",String.class);
//授权给私有和受保护方法,使之可以被访问
m3.setAccessible(true);
String str = (String) m3.invoke(clazz.newInstance(),"hjh");
System.out.println(str);
}
}
6.Method对象的invoke是有返回值,他的返回值就是目标方法执行的返回值
总结:
有了class对象之后,无所不能.