java反射基礎知識

來源:互聯網
上載者:User
package com.hsj.domain;public class Person {private String username;private String password;public Person(String username, String password) {super();this.username = username;this.password = password;}public Person() {super();}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 static void main(String[] args) {if(args!=null){for(String s :args){System.out.println(s);}}System.out.println("反射main函數測試");}public void test1(char[] chars){System.out.println("test1 char[]");}public void test2(Integer[] ints){System.out.println("test2...");}public void test3(String s1,String s2){System.out.println("test3");}@Overridepublic String toString() {return this.username+":"+this.password;}/*@Overridepublic int hashCode() {// TODO Auto-generated method stubreturn super.hashCode();}*/}package com.hsj;import java.lang.reflect.Field;import java.lang.reflect.Method;import org.junit.Test;import com.hsj.domain.Person;/** *  * @author Administrator * 需求說明,有一個普通的bean對象Person類   * 1、使用反射調用Person類的空參建構函式  clazz.newInstance(); *  如果要調用有參數的建構函式使用Constructor c=clazz.getConstructor(String.class,String.class);  *  然後通過Person p2=(Person)c.newInstance("heshengjun","123456");進行執行個體化 *  2、反射出欄位username並且賦值為‘電風扇’ *  3、通過set方法給password賦值為'dianfengshan' *  4、運行Person類的main函數的反射 *  * */public class PersonRefTest {@Testpublic void test(){try {/** * 通過類的全名稱擷取位元組碼對象 */Class clazz=Class.forName("com.hsj.domain.Person");//調用類的空參建構函式進行執行個體化Person person=(Person)clazz.newInstance();//反射出欄位username,因為是私人欄位,所有調用clazz.getDeclaredField("username");方法Field username=clazz.getDeclaredField("username");//設定可以訪問的許可權,對於私人屬性,需要進行該設定username.setAccessible(true);//給某個對象的這個方法進行賦值username.set(person, "電風扇");//擷取該對象的這個值String name =(String)username.get(person);System.out.println(name);//擷取這個位元組碼的某個方法Method setPassword=clazz.getMethod("setPassword", String.class);//對於這個方法的存取權限進行判斷,如果不可訪問、設定為true狀態可以進行訪問if(!setPassword.isAccessible()){setPassword.setAccessible(true);}setPassword.invoke(person, "dianfengshan");System.out.println(person.getPassword());//對main方法進行反射,這裡需要注意的是,對於可變參數的類型,我們使用的是數群組類型,對於基本類型的數組,可以使用如int[].class,char[].class等Method main=clazz.getMethod("main", String[].class);if(!main.isAccessible()){main.setAccessible(true);}/** * 在這裡傳遞參數的時候需要注意,對於數組參數的傳遞,一般情況會對數組進行拆開為多個參數傳遞 * 可以有兩種方法進行避免 * 1、強制轉換為一個Object類型的對象,告訴編譯器不用徐拆開 * 2、使用一個object【】數組進行封裝 */main.invoke(person, (Object)new String[]{"張三"});main.invoke(person, new Object[]{new String[]{"張學友"}});} catch (Exception e) {e.printStackTrace();}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.