public class Test {
public static void main(String[] args) throws Exception {
String str = "今天天气好晴朗";
function(str);
System.out.println(str);
}
public static void function(String str) throws Exception {
//获得字符串的字节码对象
Class clazz = str.getClass();
//获得成员变量
Field file = clazz.getDeclaredField("value");
//设置权限
file.setAccessible(true);
//获得某个对象的属性
char[] cArr = (char[])file.get(str);
cArr[0] = '明';
}
}
public static void main(String[] args) throws Exception {
String str = "今天天气好晴朗";
function(str);
System.out.println(str);
}
public static void function(String str) throws Exception {
//获得字符串的字节码对象
Class clazz = str.getClass();
//获得成员变量
Field file = clazz.getDeclaredField("value");
//设置权限
file.setAccessible(true);
//获得某个对象的属性
char[] cArr = (char[])file.get(str);
cArr[0] = '明';
}
}