概念定義
什么是反射?JAVA反射機制是在運行狀態中,對于任意一個類,都能夠知道這個類的所有屬性和方法;對于任意一個物件,都能夠呼叫它的任意一個方法,這種動態獲取、呼叫物件方法的功能稱為java語言的反射機制,
反射是通過Class物件(位元組碼檔案),來知道某個類的所有屬性和方法,也就是說通過反射我們可以獲取構造器,物件,屬性,方法,
執行流程
獲取Class物件的方式
Class<Student> student = Student.class; //方式一
Class<?> forName = Class.forName("com.test.utils.reflection.Student"); //方式二
通過Class物件獲取類加載器
下面的這個類創建了多個構造方法
public class Student {
Student(String name) {
System.out.println("用default修飾的Student的含有一個String引數的構造器:"+name);
}
public Student() {
System.out.println("用public修飾的Student的無參構造器");
}
public Student(String name,int age) {
System.out.println("用public修飾的Student的含有兩個引數的構造器:"+name+age);
}
public Student(boolean sex) {
System.out.println("用public修飾的Student的含有一個引數的構造器:"+sex);
}
protected Student(int age) {
System.out.println("用protected修飾的Student的含有一個引數的構造器:"+age);
}
private Student(String name,int age,boolean sex) {
System.out.println("用private修飾的Student的含有三個引數的構造器:"+name+age+sex);
}
private void test(String name) {
System.out.println("方法呼叫成功:"+name);
}
}
public static void main(String[] args) throws Exception {
// 生成對應的類
Class student = Student.class;
System.out.println("輸出所有的public構造方法");
// 獲取當前類的所有的public構造方法
Constructor<?>[] constructors = student.getConstructors();
for (Constructor constructor: constructors) {
System.out.println(constructor);
}
System.out.println("輸出所有的構造方法");
// 獲取當前類的所有的構造方法
Constructor<?>[] declaredConstructors = student.getDeclaredConstructors();
for (Constructor constructor: declaredConstructors) {
System.out.println(constructor);
}
// 呼叫這個構造方法 private Student(String name,int age,boolean sex)
Constructor constructor = student.getDeclaredConstructor(String.class,int.class,boolean.class);
// 該方法將忽略構造器的修飾符,即可在其他package下通過反射呼叫private的構造方法
constructor.setAccessible(true);
// 實體化一個物件
Student studentInstance = (Student) constructor.newInstance("11",2,true);
System.out.println("Student物件實體:" + studentInstance);
System.out.println("獲取所有public的方法");
// 獲取當前類的所有public的方法
Method[] declaredMethods = student.getDeclaredMethods();
for (Method method:declaredMethods) {
System.out.println(method);
}
System.out.println("獲取所有的方法");
// 獲取所有的方法
Method[] methods = student.getMethods();
for (Method method:methods) {
System.out.println(method);
}
}
執行結果
輸出所有的public構造方法
public com.test.utils.reflection.Student(boolean)
public com.test.utils.reflection.Student()
public com.test.utils.reflection.Student(java.lang.String,int)
輸出所有的構造方法
private com.test.utils.reflection.Student(java.lang.String,int,boolean)
protected com.test.utils.reflection.Student(int)
public com.test.utils.reflection.Student(boolean)
com.test.utils.reflection.Student(java.lang.String)
public com.test.utils.reflection.Student()
public com.test.utils.reflection.Student(java.lang.String,int)
用private修飾的Student的含有三個引數的構造器:112true
Student物件實體:com.test.utils.reflection.Student@2280cdac
獲取所有public的方法
public static void com.test.utils.reflection.Student.main(java.lang.String[]) throws java.lang.Exception
private void com.test.utils.reflection.Student.test(java.lang.String)
獲取所有的方法
public static void com.test.utils.reflection.Student.main(java.lang.String[]) throws java.lang.Exception
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/249311.html
標籤:Java
上一篇:一些JavaSE學習程序中的思路整理(一)(主觀性強,持續更新中...)
下一篇:java爬蟲-初識
