Java在利用反射條件下替換英文字母中的值
Java在利用反射條件下替換英文字母中的值
(1)創(chuàng)建兩個Class:
ReflectTest類如下:
package cn.itcast.day01; import java.lang.reflect.Constructor; import java.lang.reflect.Field; public class ReflectTest { public static void main(String[] args) throws Exception { changeStringValue(pt1); System.out.println(pt1); } private static void changeStringValue(Object obj) throws Exception{ Field[] fields = obj.getClass().getFields(); for(Field field :fields){ //if(field.getType().equals(String.class)){ if(field.getType() == String.class){ //同一個字節(jié)碼用等號 而不是用equal String oldValue = (String) field.get(obj); String newValue = oldValue.replace('b','a'); field.set(obj, newValue); } } } }
ReflectPoint類如下:
package cn.itcast.day01; public class ReflectPoint { public String str1 = "ball"; public String str2 = "basketball"; public String str3 = "itcast"; public ReflectPoint(int x, int y) { super(); this.x = x; this.y = y; } public String toString(){ return str1+":"+str2+":"+str3; } public static void main(String[] args) { // TODO Auto-generated method stub } }
結果為:aall:aasketaall:itcast
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接
相關文章
Springboot集成Actuator監(jiān)控功能詳解
這篇文章主要介紹了Springboot集成Actuator監(jiān)控功能詳解,有時候我們想要實時監(jiān)控我們的應用程序的運行狀態(tài),比如實時顯示一些指標數(shù)據(jù),觀察每時每刻訪問的流量,或者是我們數(shù)據(jù)庫的訪問狀態(tài)等等,這時候就需要Actuator了,需要的朋友可以參考下2023-09-09簡單的理解java集合中的HashSet和HashTree幾個重寫方法
這篇文章主要介紹了簡單的理解java集合中的HashSet和HashTree幾個重寫方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Spring Cloud gateway 網(wǎng)關如何攔截Post請求日志
這篇文章主要介紹了Spring Cloud gateway 網(wǎng)關如何攔截Post請求日志的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07springboot 項目容器啟動后如何自動執(zhí)行指定方法
這篇文章主要介紹了springboot 項目容器啟動后如何自動執(zhí)行指定方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11在Action中以Struts2的方式輸出JSON數(shù)據(jù)的實例
下面小編就為大家?guī)硪黄贏ction中以Struts2的方式輸出JSON數(shù)據(jù)的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11JDBC連接MySql數(shù)據(jù)庫步驟 以及查詢、插入、刪除、更新等
這篇文章主要介紹了JDBC連接MySql數(shù)據(jù)庫步驟,以及查詢、插入、刪除、更新等十一個處理數(shù)據(jù)庫信息的功能,需要的朋友可以參考下2018-05-05