欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java在利用反射條件下替換英文字母中的值

 更新時間:2019年03月30日 15:42:03   作者:徐劉根  
今天小編就為大家分享一篇關于Java在利用反射條件下替換英文字母中的值,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

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)容請查看下面相關鏈接

相關文章

最新評論