java反射例項詳解
阿新 • • 發佈:2018-12-23
本文先講java反射的示例程式碼,希望看了程式碼以及分析對你有所幫助。
package org.shen.own;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
public class Hash {
...
}
package org.shen.own;
public class Reflect {
public static void main(String[] args) {
Hash hash = new Hash();
System.out.println(hash.getClass().getName());
}
}
程式執行結果:org.shen.own.Hash
補充一句:所有類的物件其實都是Class的例項。
Hash程式碼
執行結果:
org.shen.own.Hash
org.shen.own.Hash
org.shen.own.Hash
Person程式碼
Reflect程式碼
輸出結果:Person [age=20, name=shen]
但是注意一下,當我們把Person中的預設的無參建構函式取消的時候,比如自己定義只定義一個有引數的建構函式之後,會出現錯誤:
Person建構函式程式碼
如下錯誤:
錯誤程式碼
所以大家以後再編寫使用Class例項化其他類的物件的時候,一定要自己定義無參的建構函式
示例程式碼
執行結果:
public org.shen.own.Person(int)
public org.shen.own.Person()
public org.shen.own.Person(java.lang.String)
public org.shen.own.Person(int,java.lang.String)
Person [age=20, name=null]
Person [age=0, name=null]
Person [age=0, name=shenyc]
Person [age=20, name=shenyc]
介面示例程式碼
【案例1】通過一個物件獲得完整的包名和類名 |
package org.shen.own;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
public class Hash {
...
}
package org.shen.own;
public class Reflect {
public static void main(String[] args) {
Hash hash = new Hash();
System.out.println(hash.getClass().getName());
}
}
程式執行結果:org.shen.own.Hash
補充一句:所有類的物件其實都是Class的例項。
【案例2】例項化Class類物件 |
Hash程式碼
- package org.shen.own;
- public class Reflect {
- public static void main(String[] args) {
-
Class<?> hash1 = null;
- Class<?> hash2 = null;
- Class<?> hash3 = null;
- try {
- hash1 = Class.forName("org.shen.own.Hash"); //返回與帶有給定字串名的類或介面相關聯的 Class 物件
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
hash2 = new Hash().getClass();
- hash3 = Hash.class;
- System.out.println(hash1.getName());
- System.out.println(hash2.getName());
- System.out.println(hash3.getName());
- }
- }
執行結果:
org.shen.own.Hash
org.shen.own.Hash
org.shen.own.Hash
【案例3】通過Class例項化其他類的物件 |
Person程式碼
- package org.shen.own;
- public class Person {
- private int age;
- private String name;
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Person [age=" + age + ", name=" + name + "]";
- }
- }
Reflect程式碼
- package org.shen.own;
- public class Reflect {
- public static void main(String[] args) {
- Class<?> demo = null;
- try {
- demo = Class.forName("org.shen.own.Person");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- Person person = null;
- try {
- person = (Person)demo.newInstance();//這裡不例項化 會報空指標異常
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- person.setAge(20);
- person.setName("shen");
- System.out.println(person);
- }
- }
輸出結果:Person [age=20, name=shen]
但是注意一下,當我們把Person中的預設的無參建構函式取消的時候,比如自己定義只定義一個有引數的建構函式之後,會出現錯誤:
Person建構函式程式碼
- public Person(int age, String name) {
- super();
- this.age = age;
- this.name = name;
- }
如下錯誤:
錯誤程式碼
- java.lang.InstantiationException: org.shen.own.Person
- at java.lang.Class.newInstance0(Class.java:340)
- at java.lang.Class.newInstance(Class.java:308)
- at org.shen.own.Reflect.main(Reflect.java:13)
- Exception in thread "main" java.lang.NullPointerException
- at org.shen.own.Reflect.main(Reflect.java:19)
所以大家以後再編寫使用Class例項化其他類的物件的時候,一定要自己定義無參的建構函式
【案例】通過Class呼叫其他類中的建構函式 (也可以通過這種方式通過Class建立其他類的物件) |
示例程式碼
- package org.shen.own;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- public class Reflect {
- public static void main(String[] args) {
- Class<?> demo = null;
- try {
- demo = Class.forName("org.shen.own.Person");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- Person per1 = null;
- Person per2 = null;
- Person per3 = null;
- Person per4 = null;
- Constructor<?>[] cons = demo.getConstructors();//獲取實體類的建構函式 共有四個
- int i;
- for(i=0;i<cons.length;i++){
- System.out.println(cons[i]);
- }
- try {
- per1 = (Person) cons[0].newInstance(20);
- per2 = (Person) cons[1].newInstance();
- per3 = (Person) cons[2].newInstance("shenyc");
- per4 = (Person) cons[3].newInstance(20,"shenyc");
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- System.out.println(per1);
- System.out.println(per2);
- System.out.println(per3);
- System.out.println(per4);
- }
- }
執行結果:
public org.shen.own.Person(int)
public org.shen.own.Person()
public org.shen.own.Person(java.lang.String)
public org.shen.own.Person(int,java.lang.String)
Person [age=20, name=null]
Person [age=0, name=null]
Person [age=0, name=shenyc]
Person [age=20, name=shenyc]
『案例』返回一個類實現的介面: |
介面示例程式碼
- package org.shen.own;
- public class Person1 implements China{
- public Person1(){}
- public Person1(String sex){
- this.sex = sex;
- }
- @Override
- public void sayChina() {
- System.out.println("say ,hello , china!");
- }
- @Override
- public void sayHello(String name, int age) {
- System.out.println(name + " --- " + age);
- }
- private String sex;
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
- package org.shen.own;
- public interface China {
- public static final String name = "Shenyc";
- public static int age = 20;
- public void sayChina();
- public void sayHello(String name,int age);
- }
- package org.shen.own;
- public class Reflect {
- public static void main(String[] args) {
- Class<?> cls = null;
- try {
- cls = Class.forName("org.shen.own.Person1"); //拿到Person1
- }