public static void main(String[] args)throws Exception{
String str1="abc";
Class cls1=str1.getClass();
Class cls2=String.class;
Class cls3=null;
try {
cls3 = Class.forName("java.lang.String");//????????????
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
System.out.println(cls1 == cls2);//????????????????
System.out.println(cls1 == cls3);
//????????????г????????????и????Class?????????:int[]??void...
System.out.println(cls1.isPrimitive());//isPrimitive?÷?????String?????????????ж?String???????????????????????????flase
System.out.println(int.class.isPrimitive());//int ??????????????
System.out.println(int.class == Integer.class);//Integer ????????????????????????
System.out.println(int.class == Integer.TYPE);//Integer?????????????????TYPE??
System.out.println(int[].class.isPrimitive());//?????????????
System.out.println(int[].class.isArray());//?ж????Class?????????
/*
* ???з???
* ???????Java???е????????????????java??
* ???磺
*  ???Java???????Class???????????????????е??????????????????????????????????????????????java?????????
*/
//??????????????
Student st=new Student("a"??5);
Field fd1=st.getClass().getField("age");
System.out.println(fd1.get(st));
//???????????????setAccessible ???????÷?????????е???
Field fd2=st.getClass().getDeclaredField("name");
fd2.setAccessible(true);//????????
System.out.println(fd2.get(st));
//???????
Method metchar=Student.class.getMethod("show"??int.class??int.class);
System.out.println(metchar.invoke(st??1??2));//invoke???????????????????????????????
changeStringvalue(st);
System.out.print(st);
//?????? ???????е?????б?
Class classType1=Student.class;//?????????????class???????
//Class classType=Class.forName("FanSheDemo.Student");//?????????????
Method[] met=classType1.getDeclaredMethods();
for(Method m:met){
System.out.println(m);
}
}
//????????????
private static void changeStringvalue(Object o){
Field[] fd=o.getClass().getFields();
for(Field f:fd){
if(f.getType()==String.class){
try {
String aldValue=(String)f.get(o);
String newValue=aldValue.replace('a'?? '1');
f.set(o??newValue);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}